Skip to content

November 17, 2015

2

Clear SCCM Cache by Deploying a Package

A while back, I wrote a simple PowerShell script that will clear the cache on a SCCM client machine (I will post this script for your reference at the end of this blog).

I mostly used this script during a large Task Sequences (ie. during a build and capture task sequence that installs a lot of software), since the cache would fill completely and then fail the Task Sequences (it appears that SCCM will not override any items in cache during the task sequence). This never presented an issue since I always ran the script directly from the Distribution Point.

However, there is one caveat when executing this script by deploying a SCCM package. It is important that when you deploy the package to use the “Run from Distribution Point” option. If you do not do this, the script will fail. This is because it will be running from the cache that you are trying to clear.

Here is the script I use:

Example Usage to use with SCCM Program: powershell.exe -Command “.\ClearConfigMgrCache.ps1 -DeletePersistent $true”

param(
 [bool] $DeletePersistent
)
$CMObject = New-Object -ComObject "UIResource.UIResourceMgr"
$CMCacheObjects = $CMObject.GetCacheInfo()
$CMCacheElements = $CMCacheObjects.GetCacheElements()
foreach ($CMCacheElement in $CMCacheElements)
{
    if ($DeletePersistent -eq $true)
    {
        $CMCacheObjects.DeleteCacheElementEx($CMCacheElement.CacheElementId, $true)
    }
    else
    {
        $CMCacheObjects.DeleteCacheElement($CMCacheElement.CacheElementId)
    }
}
2 Comments Post a comment
  1. Francois
    Nov 19 2017

    Or a solution might be to copy the script first and then run the script locally?

    Reply
    • Brent
      Nov 21 2017

      That might work; I haven’t tested that scenario. If you run locally and not from distribtion point you could still run into an issue with the copy script files still being “in use” while the Clear script works (unless the SCCM program itself contains the copy and execute commands and you don’t have to have source files). If you test this, I would be interested in knowing if it is successful.

      Reply

Share your thoughts, post a comment.

(required)
(required)

Note: HTML is allowed. Your email address will never be published.

Subscribe to comments