-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathClear-CMClientCache.ps1
122 lines (105 loc) · 4.66 KB
/
Clear-CMClientCache.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
<#
.SYNOPSIS
Clear the cache content on SCCM clients.
.DESCRIPTION
Clear the cache content on SCCM clients using the COM object.
.PARAMETER ComputerName
Enter a computer name
.PARAMETER CacheAgeinDays
Enter a number in days of the last time cache was referenced.
.PARAMETER Force
Clears persisted cache items.
.EXAMPLE
Clear-CMClientCache -ComputerName DESKTOP01
Clears all SCCM client non-persistent cache content
.EXAMPLE
Clear-CMClientCache -ComputerName DESKTOP01,DESKETOP01 -CacheAgeinDays 30 -Force
Clears all SCCM client cache content that hasn't been referenced in 30 days including persistent cache items.
.NOTES
Created by: Jason Wasser @wasserja
Modified: 7/11/2017 11:43:18 AM
#>
function Clear-CMClientCache {
[CmdletBinding(SupportsShouldProcess = $true)]
param (
[string[]]$ComputerName = $env:COMPUTERNAME,
[int]$CacheAgeinDays = 0,
[switch]$Force
)
begin {
function Clean-ClientCache {
param (
[int]$CacheAgeinDays,
[bool]$WhatIf,
[bool]$Force
)
$VerbosePreference = 'Continue'
Write-Verbose "Beginning clearing of SCCM Cache older than $CacheAgeinDays days"
$UIResourceMgr = New-Object -ComObject UIResource.UIResourceMgr
$Cache = $UIResourceMgr.GetCacheInfo()
$CacheElements = $Cache.GetCacheElements() | Where-Object -FilterScript {$_.LastReferenceTime -lt (Get-Date).AddDays( - $CacheAgeInDays)}
if ($CacheElements) {
if ($CacheElements.Count) {
Write-Verbose -Message "Found $($CacheElements.Count) cache elements on $($env:COMPUTERNAME) continuing to clear the elements in the cache."
}
else {
Write-Verbose -Message "Found a cache element on $($env:COMPUTERNAME) continuing to clear the elements in the cache."
}
foreach ($Element in $CacheElements) {
if ($WhatIf) {
if ($Force) {
Write-Verbose "What if: Forcing deletion of CacheElement with PackageID $($Element.ContentID) from $($Element.Location)"
}
else {
Write-Verbose "What if: Deleting CacheElement with PackageID $($Element.ContentID) from $($Element.Location)"
}
}
else {
if ($Force) {
Write-Verbose "Forcing deletion of CacheElement with PackageID $($Element.ContentID) from $($Element.Location)"
$Cache.DeleteCacheElementEx($Element.CacheElementID, $true)
}
else {
Write-Verbose "Deleting CacheElement with PackageID $($Element.ContentID) from $($Element.Location)"
$Cache.DeleteCacheElement($Element.CacheElementID)
}
}
}
}
else {
Write-Verbose -Message "No cache elements found on $($env:COMPUTERNAME)"
}
}
}
process {
foreach ($Computer in $ComputerName) {
Write-Verbose -Message "Attempting to clear SCCM client cache on $Computer older than $CacheAgeinDays days"
if ($PSBoundParameters.ContainsKey('WhatIf')) {
Write-Verbose 'WhatIf detected'
$WhatIf = $true
if ($Force) {
Write-Verbose 'Force detected'
$Force = $true
Invoke-Command -ComputerName $Computer -ScriptBlock ${function:Clean-ClientCache} -ArgumentList $CacheAgeinDays, $WhatIf, $Force
}
else {
Invoke-Command -ComputerName $Computer -ScriptBlock ${function:Clean-ClientCache} -ArgumentList $CacheAgeinDays, $WhatIf
}
}
else {
Write-Verbose 'WhatIf not detected'
$WhatIf = $false
if ($Force) {
Write-Verbose -Message 'Force detected'
$Force = $true
Invoke-Command -ComputerName $Computer -ScriptBlock ${function:Clean-ClientCache} -ArgumentList $CacheAgeinDays, $WhatIf, $Force
}
else {
Invoke-Command -ComputerName $Computer -ScriptBlock ${function:Clean-ClientCache} -ArgumentList $CacheAgeinDays
}
}
}
}
end {
}
}