-
Notifications
You must be signed in to change notification settings - Fork 0
/
PowerShell.ps1.txt
38 lines (33 loc) · 1.17 KB
/
PowerShell.ps1.txt
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
# Get the SCCM cache path
$SCCMCachePath = ([wmi]"ROOT\ccm\SoftMgmtAgent:CacheConfig.ConfigKey='Cache'").Location
# Check if the cache path exists and is accessible
if (!(Test-Path $SCCMCachePath))
{
Write-Error "The SCCM cache path does not exist or is not accessible: $SCCMCachePath"
return
}
# Get cache objects that have not been referenced for more than 30 days
$StaleCacheObjects = Get-WmiObject -Query "SELECT * FROM CacheInfoEx" -Namespace "ROOT\ccm\SoftMgmtAgent" | Where-Object { ([datetime](Date) - ([System.Management.ManagementDateTimeConverter]::ToDateTime($_.LastReferenced))).Days -gt 30 }
# Delete stale cache objects from disk
foreach ($StaleCacheObject in $StaleCacheObjects)
{
try
{
Remove-Item -Path $StaleCacheObject.Location -Recurse -Force
}
catch [System.Exception]
{
Write-Error "Failed to delete stale cache object from disk: $($StaleCacheObject.Location)"
}
}
# Delete stale cache objects from WMI
try
{
$StaleCacheObjects | Remove-WmiObject
}
catch [System.Exception]
{
Write-Error "Failed to delete stale cache objects from WMI."
}
# Get all cached folders from disk
$