Skip to content

Commit ccb84d5

Browse files
authored
Update Clear-CCMCache.ps1
1 parent bd7c6b0 commit ccb84d5

File tree

1 file changed

+87
-13
lines changed

1 file changed

+87
-13
lines changed

Clear-CCMCache.ps1

Lines changed: 87 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ This script is designed for administrators and developers who need to manage the
3737
3838
#>
3939

40+
# Ensure the script runs with administrative privileges
41+
#Requires -RunAsAdministrator
42+
43+
[CmdletBinding(PositionalBinding=$False)]
4044
param(
4145
[switch]$Detect,
4246
[switch]$Clean,
@@ -71,33 +75,103 @@ if (-not $Detect -and -not $Clean) {
7175
$Clean = $true
7276
}
7377

78+
# Check PowerShell version
79+
if ($PSVersionTable.PSVersion.Major -ge 7) {
80+
Write-Error "This script does not support PowerShell 7.x. Please use Windows PowerShell 5.1. Exiting..."
81+
exit 1
82+
}
83+
7484
# Get CCMCache path
75-
$CachePath = ([wmi]"ROOT\ccm\SoftMgmtAgent:CacheConfig.ConfigKey='Cache'").Location
85+
try {
86+
$CachePath = (Get-WmiObject -Namespace "ROOT\ccm\SoftMgmtAgent" -Class "CacheConfig" -Filter "ConfigKey='Cache'").Location
87+
} catch {
88+
Write-Error "Failed to retrieve CCMCache path. Exiting script."
89+
exit 1
90+
}
91+
92+
# Test if the $CachePath was successfully retrieved and exit script if not
93+
if ([string]::IsNullOrEmpty($CachePath)) {
94+
Write-Error "CachePath is null or empty. Exiting script."
95+
exit 1
96+
}
7697

7798
# Get items not referenced for more than the specified days
78-
$OldCache = Get-WmiObject -Query "SELECT * FROM CacheInfoEx" -Namespace "ROOT\ccm\SoftMgmtAgent" | Where-Object { ([datetime]::Now - ([System.Management.ManagementDateTimeConverter]::ToDateTime($_.LastReferenced))).Days -gt $Days }
99+
$OldCache = Get-WmiObject -Query "SELECT * FROM CacheInfoEx" -Namespace "ROOT\ccm\SoftMgmtAgent" | Where-Object {
100+
($([datetime]::Now) - ([System.Management.ManagementDateTimeConverter]::ToDateTime($_.LastReferenced))).Days -gt $Days
101+
}
79102

80103
if ($Detect) {
81-
# Report old items
82-
if ($OldCache) { $false } else { $true }
104+
if ($OldCache) {
105+
Write-Output "Old and unused content detected:"
106+
$OldCache | Select-Object Location, LastReferenced
107+
} else {
108+
Write-Output "No old or unused content found."
109+
}
83110
}
84111

85112
if ($Clean) {
86-
# Delete items on disk
87-
$OldCache | ForEach-Object { Remove-Item -Path $_.Location -Recurse -Force -ErrorAction SilentlyContinue }
88-
89-
# Delete items in WMI
90-
$OldCache | Remove-WmiObject
113+
if ($OldCache) {
114+
# Delete items on disk
115+
$OldCache | ForEach-Object {
116+
try {
117+
Remove-Item -Path $_.Location -Recurse -Force -ErrorAction Stop
118+
Write-Output "Deleted: $_.Location"
119+
} catch {
120+
Write-Warning "Failed to delete: $_.Location"
121+
}
122+
}
123+
124+
# Delete items in WMI
125+
$OldCache | ForEach-Object {
126+
try {
127+
$_ | Remove-WmiObject -ErrorAction Stop
128+
Write-Output "Removed WMI object: $_.Location"
129+
} catch {
130+
Write-Warning "Failed to remove WMI object: $_.Location"
131+
}
132+
}
133+
} else {
134+
Write-Output "No old or unused content to clean."
135+
}
91136

92137
# Get all cached items from disk
93-
$CacheFoldersDisk = (Get-ChildItem $CachePath).FullName
138+
try {
139+
$CacheFoldersDisk = Get-ChildItem -Path $CachePath -ErrorAction Stop | Select-Object -ExpandProperty FullName
140+
} catch {
141+
Write-Error "Failed to retrieve cached items from disk. Exiting script."
142+
exit 1
143+
}
94144

95145
# Get all cached items from WMI
96-
$CacheFoldersWMI = Get-WmiObject -Query "SELECT * FROM CacheInfoEx" -Namespace "ROOT\ccm\SoftMgmtAgent"
146+
try {
147+
$CacheFoldersWMI = Get-WmiObject -Query "SELECT * FROM CacheInfoEx" -Namespace "ROOT\ccm\SoftMgmtAgent" | Select-Object -ExpandProperty Location
148+
} catch {
149+
Write-Error "Failed to retrieve cached items from WMI. Exiting script."
150+
exit 1
151+
}
97152

98153
# Remove orphaned folders from disk
99-
$CacheFoldersDisk | ForEach-Object { if ($_ -notin $CacheFoldersWMI.Location) { Remove-Item -Path $_ -Recurse -Force -ErrorAction SilentlyContinue } }
154+
$OrphanedFolders = $CacheFoldersDisk | Where-Object { $_ -notin $CacheFoldersWMI }
155+
foreach ($folder in $OrphanedFolders) {
156+
try {
157+
Remove-Item -Path $folder -Recurse -Force -ErrorAction Stop
158+
Write-Output "Removed orphaned folder from disk: $folder"
159+
} catch {
160+
Write-Warning "Failed to remove orphaned folder: $folder"
161+
}
162+
}
100163

101164
# Remove orphaned WMI objects
102-
$CacheFoldersWMI | ForEach-Object { if ($_.Location -notin $CacheFoldersDisk) { $_ | Remove-WmiObject } }
165+
$OrphanedWMIObjects = $CacheFoldersWMI | Where-Object { $_ -notin $CacheFoldersDisk }
166+
foreach ($wmiObj in $OrphanedWMIObjects) {
167+
try {
168+
$wmiObject = Get-WmiObject -Namespace "ROOT\ccm\SoftMgmtAgent" -Query "SELECT * FROM CacheInfoEx WHERE Location='$wmiObj'"
169+
if ($wmiObject) {
170+
$wmiObject | Remove-WmiObject -ErrorAction Stop
171+
Write-Output "Removed orphaned WMI object: $wmiObj"
172+
}
173+
} catch {
174+
Write-Warning "Failed to remove orphaned WMI object: $wmiObj"
175+
}
176+
}
103177
}

0 commit comments

Comments
 (0)