-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathActiveCacheManagement-CCMCacheMaintenance.ps1
468 lines (370 loc) · 15.5 KB
/
ActiveCacheManagement-CCMCacheMaintenance.ps1
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
<#
Gary Blok | @gwblok | RecastSoftware.com
Credit Keith Garner for most of the code
.SYNOPSIS
SCCM Cache Cleaner script
.DESCRIPTION
Basic management of local Nomad Cache
Remove All packages that are missing local references
Remove All packages that are no longer being managed by CCM ( Unreferenced )
Remove All Software Updates, older than 90 days, on Large Disks
Remove All non-persistent packages, older than 30 days, on small disks
Delete software updates one by one down to 30 days or CCMCache size drops below threshold
Non-persistent items one by one down to 30 days or CCMCache size drops below threshold
.NOTES
When running with a Configuration Item:
[switch]$CIRemediate # will run Detection
[switch]$CIRemediate = $True # will run Remediation
#>
$OrgName = "Recast_IT"
[string]$Version = '2021.04.22'
$RegConfig = "HKLM:\Software\$OrgName\Cache"
$LogName = "CCMCacheChecker.log"
$LogDir = "$env:ProgramData\$OrgName\Logs"
$LogFile = "$LogDir\$LogName"
$ComponentText = "CCMCacheCheck"
$CIStatus = @()
$Module = 'CCMCacheCleaner'
$SizeRequested = 25GB
$SmallDisk = 100GB
$CMObject = New-Object -ComObject 'UIResource.UIResourceMgr'
$CMCacheObjects = $CMObject.GetCacheInfo()
$CCMCacheLocation = $CMCacheObjects.Location
#################
#region Library 1 IMPORTED FROM: [.\Common.Library.ps1]
########################################
# Library 1 IMPORTED FROM: [.\Common.Library.ps1]
# Common Library
function CMTraceLog {
[CmdletBinding()]
Param (
[Parameter(Mandatory=$false)]
$Message,
[Parameter(Mandatory=$false)]
$ErrorMessage,
[Parameter(Mandatory=$false)]
$Component = $ComponentText,
[Parameter(Mandatory=$false)]
[int]$Type,
[Parameter(Mandatory=$true)]
$LogFile = "$env:ProgramData\Logs\IForgotToNameTheLogVar.log"
)
<#
Type: 1 = Normal, 2 = Warning (yellow), 3 = Error (red)
#>
$Time = Get-Date -Format "HH:mm:ss.ffffff"
$Date = Get-Date -Format "MM-dd-yyyy"
if ($ErrorMessage -ne $null) {$Type = 3}
if ($Component -eq $null) {$Component = " "}
if ($Type -eq $null) {$Type = 1}
$LogMessage = "<![LOG[$Message $ErrorMessage" + "]LOG]!><time=`"$Time`" date=`"$Date`" component=`"$Component`" context=`"`" type=`"$Type`" thread=`"`" file=`"`">"
$LogMessage | Out-File -Append -Encoding UTF8 -FilePath $LogFile
}
function Set-AppSettingIncrement {
[cmdletbinding()]
param (
[string] $Name
)
[int]$Value = Get-AppSettings @PSBoundParameters
Set-AppSettings @PSBoundParameters -Value ($Value + 1).ToString()
}
function Get-AppSettings {
[cmdletbinding()]
param ( [string] $Name )
if ( -not $RegConfig ) { throw "missing $RegConfig" }
if ( -not ( test-path $RegConfig ) ) {
new-item -ItemType Directory -Path $RegConfig -force -ErrorAction SilentlyContinue | Out-Null
}
try { Get-ItemPropertyValue -Path $RegConfig -name $Name | Write-Output } catch {}
}
function Set-AppSettings {
[cmdletbinding()]
param ( [string] $Name, $Value )
if ( $Value -and ( $Value.GetType().Name -in 'Int32','Int64','uint32','uint64','double' ) -and ($Value -gt 3MB ) ) {
CMTraceLog -Message ("`t`tSettings: [$Name] = [$value] {0:N0} MB" -f ( $value / 1MB )) -Type 1 -LogFile $LogFile
}
else {
CMTraceLog -Message "`t`tSettings: [$Name] = [$value]" -Type 1 -LogFile $LogFile
}
if ( -not $RegConfig ) { throw "missing $RegConfig" }
if ( -not ( test-path $RegConfig ) ) {
new-item -ItemType Directory -Path $RegConfig -force -ErrorAction SilentlyContinue | Out-Null
}
Set-ItemProperty -path $RegConfig -name $Name -Value $Value | Out-Null
}
Function Exit-WithError {
[cmdletbinding()]
param (
[int] $ExitCode,
[string] $module = 'CacheUtility',
[string] $Msg
)
if ( $ExitCode -ne 0 ) {
CMTraceLog -Message "ERROR: $Msg" -Type 1 -LogFile $LogFile
New-EventLog -LogName Application -Source $module -ErrorAction SilentlyContinue
Write-EventLog -LogName Application -Source $module -EventId $ExitCode -Message $msg
}
else {
CMTraceLog -Message "$Msg" -Type 1 -LogFile $LogFile
}
Set-AppSettings -name 'LastStatusInt' -Value $ExitCode
Set-AppSettings -name 'LastStatusMsg' -Value $Msg
exit $ExitCode
}
function Approve-ObjectIfRemediate {
[cmdletbinding()]
param (
[parameter(ValueFromPipeline=$true)] $InputObject,
[string] $PropertyName
)
process {
if ( -not $CIRemediate ) {
if ( $PropertyName ) {
$Name = $InputObject | % $PropertyName
}
else {
$Name = $InputObject
}
CMTraceLog -Message "Do not remediate object, flag for use later" -Type 1 -LogFile $LogFile
$Text = $Name | out-string
CMTraceLog -Message $Text -Type 1 -LogFile $LogFile
$global:isComplaint = $Name
}
else {
$InputObject | Write-Output
}
}
}
Function Get-VolumeEx {
<#
Windows 7 does not have Get-Volume
#>
[cmdletbinding()]
param ( $DriveLetter = 'c' )
gwmi win32_logicaldisk -Filter "DeviceID='$($DriveLetter.Substring(0,1))`:'" |
Select -Property Size,FileSystem,
@{Name='SizeRemaining';Expression={$_.FreeSPace}},
@{Name='DriveLetter';Expression={$_.Caption.SubString(0,1)}},
@{Name='FileSystemLabel';Expression={$_.VolumeName}}
}
function Invoke-As64Bit {
<#
Re-Invoke Powershell, this time as a 64-bit process.
Warning, will only return 1 or 0 as last error code.
Example usage:
if ( Invoke-As64Bit -Invokation $myInvocation -arks $args ) {
write-host "finished $lastexitcode"
exit $lastexitcode
}
#>
[cmdletbinding()]
param( [parameter(Mandatory=$true)] $Invokation, $arks )
#Re-Invoke
if ($env:PROCESSOR_ARCHITEW6432 -eq "AMD64") {
if ($Invokation.Line) {
write-verbose "RUn Line: $($Invokation.Line)"
& "$env:WINDIR\sysnative\windowspowershell\v1.0\powershell.exe" -noninteractive -NoProfile $Invokation.Line
}else{
write-verbose "RUn Name: $($Invokation.InvocationName) $arks"
& "$env:WINDIR\sysnative\windowspowershell\v1.0\powershell.exe" -noninteractive -NoProfile -file "$($Invokation.InvocationName)" $arks
}
return $true
}
return $false
}
#endregion
$script:CCM = $null
function Get-CCMCachePath {
Get-CCMCacheSettings location | Write-Output
}
function Get-CCMCacheSettings {
[cmdletbinding()]
param ( [string] $Name )
Test-ForCCM
$script:CCM.GetCacheInfo().$Name | Write-Output
}
function Set-CCMCacheSettings {
[cmdletbinding()]
param ( [string] $Name, [string] $Value )
CMTraceLog -Message "`t`tSettings: [$Name] = [$value]" -Type 1 -LogFile $LogFile
Test-ForCCM
$script:CCM.GetCacheInfo().$Name = $Value
}
function Test-ForCCM {
try {
if ( -not $script:CCM ) {
$script:CCM = new-object -ComObject UIResource.UIResourceMgr -ErrorAction SilentlyContinue
}
} catch {}
if ( -not $script:CCM ) { Exit-WithError 101 'Missing CCM Installation - Service' }
}
function Get-CCMPackages {
Test-ForCCM
$script:ccm.GetCacheInfo().GetCacheElements() | write-output
}
function Test-ForPersist {
param (
[parameter(Mandatory=$true, ValueFromPipeline=$true)]
$Keys
)
begin {
$PersistList = Get-WmiObject -Namespace root\ccm\SoftMgmtAgent -class CacheInfoEx |
Where-Object { ( $_.DeploymentFlags -band 33554432 ) -eq 33554432 } |
foreach-object { $_.cacheid }
}
process {
$Keys | Where-Object { $_.CacheElementID.trim('{}') -in $PersistList } | Write-Output
}
}
function Test-ForNotPersist {
param (
[parameter(Mandatory=$true, ValueFromPipeline=$true)]
$Keys
)
begin {
$PersistList = Get-WmiObject -Namespace root\ccm\SoftMgmtAgent -class CacheInfoEx |
Where-Object { ( $_.DeploymentFlags -band 33554432 ) -eq 33554432 } |
foreach-object { $_.cacheid }
}
process {
$Keys | Where-Object { $_.CacheElementID.trim('{}') -notin $PersistList } | Write-Output
}
}
function Remove-CCMPackage {
[CmdletBinding(SupportsShouldProcess=$true)]
param (
[parameter(Mandatory=$true, ValueFromPipeline=$true)]
$Keys
)
process {
foreach ( $Key in $Keys ) {
CMTraceLog -Message "`t`tRemove $($Key.CacheElementID) / $($Key.ContentID) Age: $($key.LastReferenceTime) Size: $($Key.ContentSize / 1KB -as [int]) MB" -Type 1 -LogFile $LogFile
if (-not $CIRemediate) {
$script:CIStatus += "NotCompliant: $($Key.CacheElementID) Age: $($key.LastReferenceTime) Size: $($Key.ContentSize / 1KB) MB`r`n"
}
elseif ( $Pscmdlet.ShouldPRocess("$key.CacheElementID","Remove") ) {
CMTraceLog -Message "Actually Removing $KEY.CacheElementID" -Type 1 -LogFile $LogFile
Test-ForCCM
$script:ccm.GetCacheInfo().DeleteCacheElement($Key.CacheElementID)
}
}
}
}
Function Test-ForCCMSoftwareUpdate {
# Content in a {GUID} folder
[cmdletbinding()]
param ( [parameter(Mandatory=$true, ValueFromPipeline=$true)] $InputObject )
process { $InputObject | where-object { $_.ContentID | Select-String -Pattern '^[\dA-F]{8}-(?:[\dA-F]{4}-){3}[\dA-F]{12}$' } | write-output }
}
Function Test-ForCCMLegacyContent {
# Content in a CAS00001 folder
[cmdletbinding()]
param ( [parameter(Mandatory=$true, ValueFromPipeline=$true)] $InputObject )
process { $InputObject | where-object { $_.ContentID | Select-String -Pattern '^[\dA-Z]{3}[\dA-F]{5}$' } | write-output }
}
Function Test-ForCCMApplicationDeployment {
# Content in a Content_{GUID} folder
[cmdletbinding()]
param ( [parameter(Mandatory=$true, ValueFromPipeline=$true)] $InputObject )
process { $InputObject | where-object { $_.ContentID | Select-String -Pattern '^Content_[\dA-F]{8}-(?:[\dA-F]{4}-){3}[\dA-F]{12}$' } | write-output }
}
#region Main()
#############################################################
$DiskSize = Get-VolumeEx c | foreach-object { $_.Size }
$FreeSize = Get-VolumeEx c | foreach-object { $_.SizeRemaining }
#######################################
if ( -not ( Get-AppSettings -Name 'Exempt' | Where-Object { $_ -eq 'True' } ) ) {
Test-ForCCM
$CCMTotalSize = Get-CCMCacheSettings TotalSize
CMTraceLog -Message "CCM Total Size: $CCMTotalSize" -Type 1 -LogFile $LogFile
#######################################
#Rule: Remove All non-persistent packages, older than 30 days, on small disks
if ( $DiskSize -lt $SmallDisk ) {
Get-CCMPackages | ? { $_.LastReferenceTime.adddays(30) -lt [datetime]::now} |
Test-ForNotPersist |
Remove-CCMPackage
}
#######################################
#Rule: Remove All Software Updates, older than 90 days, on Large Disks
if ( $DiskSize -gt $SmallDisk ) {
Get-CCMPackages | ? { $_.LastReferenceTime.adddays(90) -lt [datetime]::now} |
Test-ForCCMSoftwareUpdate |
Remove-CCMPackage
}
#######################################
#Rule: Remove All packages that are no longer being managed by CCM ( Unreferenced )
$Exclude = @{}
Get-CCMPackages | foreach-object { $Exclude.add($_.Location,$_) }
Get-CCMCachePath | get-childitem | Where-object { -not ($exclude.contains( $_.FullName ) ) } |
ForEach-Object {
CMTraceLog -Message "Remove CACHE Item: $($_.FUllName)" -Type 1 -LogFile $LogFile
Remove-Item -Force -Recurse $_.FUllName
}
#######################################
#Rule: Remove All packages that are missing local references
Get-CCMPackages | where-object { -not ( test-path $_.location ) } |
Remove-CCMPackage
#######################################
$CCMCacheSize = get-childitem $CCMCacheLocation -recurse -exclude skpswi.dat | measure-object -sum length | foreach-object { $_.Sum }
$Count = 1
while (
# Rule: CCMCache greater than 10GB (on small disks), 10% (on disks 100 - 500), 50 GB (on disks greater than 500 GB)
( ( $DiskSize -gt 500GB ) -and ( $CCMCacheSize -gt 50gb ) ) -or
( ( $DiskSize -lt 100GB ) -and ( $CCMCacheSize -gt 10gb ) ) -or
( ( $CCMCacheSize -gt ( $DiskSize * 0.1 ) ) ) -or
# Rule: Free disk space is less than 10% (on disks less than 250 GB) or less than 25 GB (on disks greater than 250 GB)
( ( $DiskSize -gt 250GB ) -and ( $FreeSize -lt 25gb ) ) -or
( ( $DiskSize -lt 250GB ) -and ( $FreeSize -lt ( $DiskSize * 0.1 ) ) ) -or
( $false )
)
{
CMTraceLog -Message "Remediate $CCMCacheSize too big or Free SIze $FreeSize too small on $DiskSize ..." -Type 1 -LogFile $LogFile
# SubRule: Delete software updates one by one down to 30 days or CCMCache size drops below threshold
$Found = Get-CCMPackages |
? { $_.LastReferenceTime.adddays(30) -lt [datetime]::now} |
Test-ForCCMSoftwareUpdate |
sort LastReferenceTime |
Select-Object -first 1
if ( -not $found ) {
# SubRule: Non-persistent items one by one down to 30 days or CCMCache size drops below threshold
$Found = Get-CCMPackages |
? { $_.LastReferenceTime.adddays(30) -lt [datetime]::now} |
Test-ForNotPersist |
sort LastReferenceTime |
Select-Object -first 1
}
if ( -not $found ) {
$CIStatus += "Too much Cache $($CCMCacheSize/1GB) GB, nothing to remove!"
CMTraceLog -Message "WARNING: No packages found to remove." -Type 1 -LogFile $LogFile
break
}
$Found | Remove-CCMPackage
##############
if ( -not $CIRemediate ) {break}
$FreeSize = Get-VolumeEx c | foreach-object { $_.SizeRemaining }
$CCMCacheSize = get-childitem $CCMCacheLocation -recurse -exclude skpswi.dat | measure-object -sum length | foreach-object { $_.Sum }
CMTraceLog -Message "CacheSize: $($CCMCacheSize/1GB) GB FreeSize: $($FreeSIze/1GB) GB"-Type 1 -LogFile $LogFile
if ( $Count -gt 200 )
{
CMTraceLog -Message "BAIL" -Type 1 -LogFile $LogFile
break
}
$count += 1
}
CMTraceLog -Message "Finished: $Count" -Type 1 -LogFile $LogFile
#######################################
write-verbose "Write Summary to Registry"
Set-AppSettings -Name 'CCMTotalSize' -Value $CCMTotalSize
Set-AppSettings -Name 'CCMCacheSize' -Value (get-childitem $CCMCacheLocation -recurse -exclude skpswi.dat | measure-object -sum length | foreach-object { $_.Sum })
Set-AppSettings -Name 'CCMCacheItems' -Value (get-childitem $CCMCacheLocation | Where-Object { $_.PSISContainer } | measure-object | foreach-object { $_.Count })
}
#######################################
write-verbose "Write Summary to Registry"
Set-AppSettings -Name 'DiskSize' -Value $DiskSize
Set-AppSettings -Name 'FreeSize' -Value $FreeSize
Set-AppSettings -Name 'Version' -value $Version.ToString()
if ( -not $CIStatus ) { $CIStatus = 'Compliant' } else { $CiStatus = 'noncompliant' }
Write-host $CIStatus
Set-AppSettings -Name 'LastStatusInt' -Value 0
Set-AppSettings -Name 'LastStatusMsg' -Value $CIStatus
#endregion