@@ -37,6 +37,10 @@ This script is designed for administrators and developers who need to manage the
37
37
38
38
#>
39
39
40
+ # Ensure the script runs with administrative privileges
41
+ # Requires -RunAsAdministrator
42
+
43
+ [CmdletBinding (PositionalBinding = $False )]
40
44
param (
41
45
[switch ]$Detect ,
42
46
[switch ]$Clean ,
@@ -71,33 +75,103 @@ if (-not $Detect -and -not $Clean) {
71
75
$Clean = $true
72
76
}
73
77
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
+
74
84
# 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
+ }
76
97
77
98
# 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
+ }
79
102
80
103
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
+ }
83
110
}
84
111
85
112
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
+ }
91
136
92
137
# 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
+ }
94
144
95
145
# 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
+ }
97
152
98
153
# 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
+ }
100
163
101
164
# 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
+ }
103
177
}
0 commit comments