-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathActiveCacheManagement-CCMCacheDuplicates-Remediate.ps1
81 lines (61 loc) · 2.91 KB
/
ActiveCacheManagement-CCMCacheDuplicates-Remediate.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
<#
Gary Blok | @gwblok | RecastSoftware.com
.SYNOPSIS
Checks for Duplicate Package IDs in CCMCache and Removes
#>
$OrgName = "Recast_IT"
[string]$Version = '2021.04.22'
$LogName = "CCMCacheChecker.log"
$LogDir = "$env:ProgramData\$OrgName\Logs"
$LogFile = "$LogDir\$LogName"
$ComponentText = "CCMCacheDuplicates"
$DetectionMode=$true
if (!(Test-Path -Path $LogDir)){New-Item -Path $LogDir -ItemType Directory -Force}
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
}
#Delete Duplicate Instances of Package in CCMCache while keeping the most updated.
#Connect to CCM ComObject
$CMObject = New-Object -ComObject 'UIResource.UIResourceMgr'
$CMCacheObjects = $CMObject.GetCacheInfo()
#Get Packages with more than one instance
$Packages = $CMCacheObjects.GetCacheElements() | Group-Object -Property ContentID | ? {$_.count -gt 1}
#Go through the Duplicate Package and do magic.
ForEach ($Package in $Packages)
{
$PackageID = $Package.Name
$PackageCount = ($CMCacheObjects.GetCacheElements() | Where-Object {$_.ContentID -eq "$PackageID"}).Count
Write-Host "Package: $PackageID has $PackageCount instances" -ForegroundColor Green
$DuplicateContent = $CMCacheObjects.GetCacheElements() | Where-Object {$_.ContentID -eq "$PackageID"}
$ContentVersion = $DuplicateContent.ContentVersion | Sort-Object
$HighestContentID = $ContentVersion | measure -Maximum
$NewestContent = $DuplicateContent | Where-Object {$_.ContentVersion -eq $HighestContentID.Maximum}
write-host "Most Updated Package for $PackageID = $($NewestContent.ContentVersion)" -ForegroundColor Green
$CMCacheObjects.GetCacheElements() | Where-Object {$_.ContentID -eq $PackageID -and $_.ContentVersion -ne $NewestContent.ContentVersion } | ForEach-Object {
$CMCacheObjects.DeleteCacheElement($_.CacheElementID)
Write-Host "Deleted: Name: $($_.ContentID) Version: $($_.ContentVersion)" -BackgroundColor Red
CMTraceLog -Message "Deleted Name: $($_.ContentID) Version: $($_.ContentVersion)" -Type 1 -LogFile $LogFile
}
}