In the past I posted how to configure or clear the SCCM client cache using Powershell scripts, but that method required a separate script for each cache size you might want to set. This new script allows on-the-fly modification of the CCM Cache from a task sequence using a single SCCM package containing this new Powershell script.
To use this script, you will need to create a package in SCCM with the source contents containing this script. You do not need to create a program for the package. Make sure the package can be used by task sequences without being deployed. After distributing the package, just call it from a task sequence using a “Run Command Line” step referencing the package.
Assuming you save the script as “sccmCache.ps1”, any of these usage examples will work:
1 2 3 4 5 6 |
powershell -executionpolicy bypass -file sccmCache.ps1 -cacheSize 5120 -clearCache yes powershell -executionpolicy bypass -file sccmCache.ps1 5120 clear powershell -executionpolicy bypass -file sccmCache.ps1 -cacheSize 5120 powershell -executionpolicy bypass -file sccmCache.ps1 5120 powershell -executionpolicy bypass -file sccmCache.ps1 -clearCache yes powershell -executionpolicy bypass -file sccmCache.ps1 clear |
And here is the Powershell script:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
<# This script modifies the SCCM client cache on the computer. Default cache size is 5120 MB. Usage (any of the following examples will work): powershell -executionpolicy bypass -file sccmCache.ps1 -cacheSize 5120 -clearCache yes powershell -executionpolicy bypass -file sccmCache.ps1 5120 clear powershell -executionpolicy bypass -file sccmCache.ps1 -cacheSize 5120 powershell -executionpolicy bypass -file sccmCache.ps1 5120 powershell -executionpolicy bypass -file sccmCache.ps1 -clearCache yes powershell -executionpolicy bypass -file sccmCache.ps1 clear If there's a cacheSize parameter, it must come before the clearCache parameter. Script created by Boris Kagan. https://www.boriskagan.net #> # read variables passed from the command line when launching this script param($cacheSize, $clearCache) # if cacheSize parameter is a non-empty number, set the new cache size if ([bool]($cacheSize -as [double])) { Write-Host "Setting new SCCM client cache size: $cacheSize" $UIResourceMgr = New-Object -ComObject UIResource.UIResourceMgr $Cache = $UIResourceMgr.GetCacheInfo() $Cache.TotalSize = $cacheSize # otherwise if it's a non-empty string, it must be the string signal to clear cache } elseif ([bool]($cacheSize -as [string])) { $clearCache = $cacheSize } # if clearCache parameter is a non-empty string, clear the cache if ([bool]($clearCache -as [string])) { Write-Host "Clearing SCCM client cache" $CacheElements = $Cache.GetCacheElements() foreach ($Element in $CacheElements) { $Cache.DeleteCacheElement($Element.CacheElementID) } } |
Let me know your thoughts, suggestions, or issues in the comments below.