XenDesktop with PvD: CCMCache

Running Windows 7 desktops with personal vdisks and SCCM can be a difficult task, especially with the amount of space and data SCCM uses to cache it’s installs/uninstalls. A typical software package can range anywhere from 10MB to 1GB+, this is where the following script comes in handy.

What the script does is cleans the ‘C:\Windows\ccmcache‘ directory based on the age of the content, also removing persisted data. In my case, I’ve set the age to 0 because I want to remove anything that’s not needed.

User ‘gpedit.msc‘ to set a Computer startup script to run the following script (save as .vbs).

'Const declaration
Const ForAppending = 8
Const ForReading = 1

'++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

'Variable declaration
on error resume next
dim oUIResManager
dim oCache
dim oCacheElement
dim oCacheElements
dim objToday  'Todays date
dim objfso    'File System Object

'Set File System Object
'------------------------------------------------------------------------------------------------------------------------------------
Set objfso = CreateObject("Scripting.FileSystemObject") 

'Set log file strPath
'------------------------------------------------------------------------------------------------------------------------------------
strLogPath = "c:\logs\CCMCache.log"

'Create or open log file
'------------------------------------------------------------------------------------------------------------------------------------
If objfso.FileExists(strLogPath) Then
 if objfso.getfile(strLogPath).size >1024000 then
  If objfso.FileExists(strLogPath & ".old") then
   objfso.DeleteFile(strLogPath & ".old")
   objfso.MoveFile  strLogPath,strLogPath & ".old"
  else
  objfso.MoveFile strLogPath,strLogPath & ".old"
  end if
 end if
 Set objLogFile = objfso.OpenTextFile(strLogPath, ForAppending, True)
else
 Set objLogFile = objfso.CreateTextFile(strLogPath)
end if
objLogFile.WriteLine("******************************************************************************")
objLogFile.WriteLine("Script starting at " & now)

set oUIResManager = createobject("UIResource.UIResourceMgr")

if oUIResManager is nothing then
   objLogFile.WriteLine("UIResManager is set to nothing")
 wscript.quit
end if

set oCache=oUIResManager.GetCacheInfo()

if oCache is nothing then
   set oUIResManager=nothing
 objLogFile.WriteLine("oCache is set to nothing")
 wscript.quit
end if

lngFreeSpace = oCache.FreeSize
lngTotalSpace = oCache.TotalSize
lngStartUsedSpace = lngTotalSpace - lngFreeSpace
objLogFile.WriteLine("Total cache size is " & lngTotalSpace & "MB")
objLogFile.WriteLine("Amount of space used is " & lngStartUsedSpace & "MB")

set oCacheElements=oCache.GetCacheElements
objToday =  cdate(date)
objLogFile.WriteLine(vbtab & "ContentID" & vbtab & "ContentSize" & vbtab & "ContentVersion" & vbtab &  "LastReferenceTime" & vbtab & "ReferenceCount"& vbtab & "Location" )
for each oCacheElement in oCacheElements
   objLogFile.WriteLine(oCacheElement.CacheElementID)
   objLogFile.WriteLine(vbtab & oCacheElement.ContentID & vbtab & oCacheElement.ContentSize & vbtab & vbtab & vbtab & oCacheElement.ContentVersion & vbtab &  oCacheElement.LastReferenceTime & vbtab & oCacheElement.ReferenceCount & vbtab & oCacheElement.Location )
   iRefCount=(oCacheElement.ReferenceCount)
   iDateDiff=datediff ("d",oCacheElement.LastReferenceTime,objToday)
   objLogFile.WriteLine(vbtab & "ReferenceCount is " & iRefCount &"-"&  iDateDiff)
 objLogFile.WriteLine(vbtab & "Number of day since this cached folder was used " &  iDateDiff)
   if iRefCount = 0 and iDateDiff >= 0 then
      objLogFile.WriteLine(vbtab & "Mark for delete")
      'oCache.DeleteCacheElement(oCacheElement.CacheElementID)
	  oCache.DeleteCacheElementEx oCacheElement.CacheElementID, true
   end if
next

lngFreeSpace = oCache.FreeSize

objLogFile.WriteLine("Currently in Cache size is " & lngTotalSpace - lngFreeSpace & "MB")

lngEndUsedSpace = lngTotalSpace - lngFreeSpace
objLogFile.WriteLine("Total amount of space freed up was " & lngStartUsedSpace - lngEndUsedSpace & "MB")

set oCacheElements=nothing
set oUIResManager=nothing
set oCache=nothing

'Closing log file
objLogFile.WriteLine("Script finished at " & now)
objLogFile.Close()

Resources: http://social.technet.microsoft.com/…clearing-ccm-cache?forum=configmgrswdist

I've been working in the IT Industry since 2001 and I've acquired the ever-changing taste for Citrix since 2009. I'm currently a Citrix Certified Integration Architect working as an independent consultant. I've worked for many large businesses in various industries. I truly enjoy what I do and I strive to exceed the expectations of the companies for which I work.

Leave a Reply