Nick Panaccio
12/10/2025, 1:49 AMRob Zylowski
12/10/2025, 2:46 AMRob Zylowski
12/10/2025, 2:47 AMJon Bucud
12/10/2025, 3:36 AM[string]$appVendor = 'Microsoft'
[string]$appName = "SCCM Agent"
[string]$appInstallPath = "$env:windir\CCM"
[string]$appService = 'CcmExec'
[string]$appRegKey = "$hklm_software\Microsoft\SystemCertificates\SMS\Certificates"
$CryptoPath = "C:\ProgramData\Microsoft\Crypto"
$CryptoKey = "Keys"
$OldCrpytoKey = "KeysOLD"
$CryptoKeyPath = $CryptoPath + "\" + $CryptoKey
$OldCrpytoKeyPath = $CryptoPath + "\" + $OldCrpytoKey
Remove-Item -Path "$env:windir\SMSCFG.ini" -Force -ErrorAction SilentlyContinue
Remove-Item -Path HKLM:\Software\Microsoft\SystemCertificates\SMS\Certificates\* -Force
Remove-Item -Path "C:\ProgramData\Microsoft\Crypto\KeysOLD" -Force -Recurse
Get-CimInstance -Namespace root\ccm\locationservices -Class TrustedRootKey | Remove-CimInstance
Get-CimInstance -Namespace root\ccm\invagt -Class InventoryActionStatus | Where-Object { $_.InventoryActionID -eq "{00000000-0000-0000-0000-000000000001}" } | Remove-CimInstance
Get-CimInstance -Namespace root\ccm\scheduler -Class CCM_Scheduler_History | Where-Object { $_.ScheduleID -eq "{00000000-0000-0000-0000-000000000001}" } | Remove-CimInstance
Rename-Item -Path $CryptoKeyPath -NewName $OldCrpytoKey -Force
New-Item -Path $CryptoKeyPath -ItemType Directory
Get-Acl -Path $OldCrpytoKeyPath | Set-Acl -Path $CryptoKeyPath
function Remove-CCMCache {
[CmdletBinding()]
$UIResourceMgr = New-Object -ComObject UIResource.UIResourceMgr
$Cache = $UIResourceMgr.GetCacheInfo()
$CacheElements = $Cache.GetCacheElements()
foreach ($Element in $CacheElements) {
Write-BISFLog -Msg "$appVendor $appName deleted Cache Element with PackageID $($Element.ContentID)"
Write-BISFLog -Msg "from folder $($Element.Location)"
$Cache.DeleteCacheElement($Element.CacheElementID)
}
}Robert Magasi
12/10/2025, 7:47 AMNick Panaccio
12/10/2025, 11:44 AM$Log = "C:\Core\logs\Generalize.log"
Function Write-ToLog {
Param (
[Parameter(Mandatory = $True, ValueFromPipeline = $True)] [String]$Message,
[Parameter(Mandatory = $False)] [Bool]$ErrTrap = $False
)
$Message = [DateTime]::Now.ToString("[MM/dd/yyy HH:mm:ss]")+ " $($Env:UserDomain)\$($Env:Username): " + $Message
If ($ErrTrap) {
Write-Host $Message -ForegroundColor Red -BackgroundColor Yellow
} Else {
Write-Host $Message -ForegroundColor Green
}
$Message | Out-File $Log -Append
}
Write-ToLog "Stopping the CM client"
Get-Service -Name "CcmExec" | Stop-Service -WarningAction SilentlyContinue -ErrorVariable StopCcm | Out-Null
If ((Get-Service -Name "CcmExec").Status -ne "Stopped") {
Write-ToLog "Terminating the CcmExec process"
Get-Process -Name "CcmExec" | Stop-Process -Force | Out-Null
}
Write-ToLog "Changing the CcmExec service to 'Automatic (Delayed Start)'"
$Proc = Start-Process -FilePath "sc.exe" -ArgumentList "config CcmExec Start= Delayed-Auto" -WorkingDirectory $PSScriptRoot -Wait -PassThru
Write-ToLog "Deleting the SMS Certs"
$Proc = Start-Process -FilePath "Certmgr.exe" -ArgumentList "/del /all /c /s /r localmachine SMS" -WorkingDirectory $PSScriptRoot -Wait -PassThru
Write-ToLog "Deleting the SMS RSA Keyfile"
Remove-Item "C:\ProgramData\Microsoft\Crypto\RSA\MachineKeys\19c5cf9c7b5dc9de3e548adb70398402_9e2ae6f6-0938-42e2-a85a-4ddfaa4b92cf" -Force -ErrorAction SilentlyContinue | Out-Null
Write-ToLog "Deleting SmsCfg.ini"
Remove-Item "C:\Windows\SmsCfg.ini" -Force -ErrorAction SilentlyContinue | Out-Null
Write-ToLog "Validating Generalization success"
$Output += Invoke-Command -ScriptBlock { sc.exe qc CcmExec }
$i = New-Object -TypeName PSObject
$Output | Select-String -Pattern "START_TYPE" | ForEach-Object {
Add-Member -InputObject $i -MemberType NoteProperty -Name (([String]$_).Trim() -Split " :")[0] -Value (([String]$_).Trim() -Split " :")[1] | Out-Null
}
$SvcStart = $i.START_TYPE
$Certs - Get-ChildItem HKLM:\Software\Microsoft\SystemCertificates\SMS\Certificates
$Cfg = Get-Item "C:\Windows\SmsCfg.ini" 2>$Null
If (!($Certs) -and (!($Cfg)) -and ($SvcStart -like "*DELAYED*")) {
Write-ToLog "Generalization succeeded!"
Write-ToLog "Shutting down device in 20 seconds"
Start-Process -FilePath "shutdown.exe" -ArgumentList "/s /t 20" -NoNewWindow -PassThru | Out-Null
} Else {
Write-ToLog "Generalization failed!" -ErrTrap $True
}
ExitNick Panaccio
12/10/2025, 11:48 AMRob Zylowski
12/10/2025, 12:06 PMNick Panaccio
12/10/2025, 12:07 PMNick Panaccio
12/10/2025, 12:42 PM((Get-WmiObject -Namespace root\ccm -query "Select ClientID from CCM_Client").ClientID).Split(':')[1]Rob Zylowski
12/10/2025, 1:03 PMNick Panaccio
12/10/2025, 1:18 PMNick Panaccio
12/10/2025, 1:25 PMRob Zylowski
12/10/2025, 1:27 PMNick Panaccio
12/10/2025, 1:28 PMNick Panaccio
12/10/2025, 1:28 PM