Skip to content

Instantly share code, notes, and snippets.

@brianfgonzalez
Last active June 28, 2023 15:35
Show Gist options
  • Save brianfgonzalez/2e1a3ebc86213d807b87d4bc4e866f0c to your computer and use it in GitHub Desktop.
Save brianfgonzalez/2e1a3ebc86213d807b87d4bc4e866f0c to your computer and use it in GitHub Desktop.
# Version 1.1 - BFG
# cmdline: powershell.exe -ExecutionPolicy Bypass -File zWinUpdateLocalCleanup.ps1
$transcriptLogFilePath = "C:\Windows\CCM\Logs\zWinUpdateLocalCleanup.log"
Start-Transcript -Path $transcriptLogFilePath -Force
Write-Host "Starting script execution..."
Write-Host "Running sfc /scannow"
#Start-Process -FilePath $env:windir\system32\sfc.exe -ArgumentList '/scannow' -Wait -Verbose
Write-Host "Running dism /online /cleanup-image /restorehealth.. see \windows\logs\dism\dism.log"
#Start-Process -FilePath $env:windir\system32\dism.exe -ArgumentList '/online /cleanup-image /restorehealth' -Wait -Verbose
Write-Host "Setting wu services to startup type to manual..."
'CryptSvc','wuauserv','bits' | % { Set-Service -Name $_ -StartupType Manual -Verbose }
Write-Host "Now stopping services..."
Stop-Service -Name wuauserv -Force -Verbose
Stop-Service -Name cryptSvc -Force -Verbose
Stop-Service -Name bits -Force -Verbose
Stop-Service -Name msiserver -Force -Verbose
Write-Host "Checking for previous sd bu and deleting it..."
if (Test-Path C:\WINDOWS\SoftwareDistribution.bak)
{
Remove-Item C:\WINDOWS\SoftwareDistribution.bak -Force -Recurse -Verbose
}
Write-Host "Moving SD directory..."
Move-Item C:\WINDOWS\SoftwareDistribution C:\WINDOWS\SoftwareDistribution.bak -Force -Verbose
Write-Host "Checking for previous cr2 bu and deleting it..."
if (Test-Path C:\Windows\System32\catroot2.bak)
{
Remove-Item C:\Windows\System32\catroot2.bak -Force -Recurse -Verbose
}
Write-Host "Moving catroot2..."
Move-Item C:\Windows\System32\catroot2 C:\Windows\System32\catroot2.bak -Force -Verbose
Write-Host "Post current reg settings to log..."
Get-ChildItem HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate -Verbose
Get-ChildItem HKLM:\SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate -Verbose
Write-Host "Deleting registry keys...may toss errors if regs are not found this is ok."
Remove-ItemProperty -Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate -Name PingID -Force -Verbose
Remove-ItemProperty -Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate -Name AccountDomainSid -Force -Verbose
Remove-ItemProperty -Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate -Name SusClientId -Force -Verbose
Remove-ItemProperty -Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate -Name SusClientIDValidation -Force -Verbose
Write-Host "rePost after reg removals to log..."
Get-ChildItem HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate -Verbose
Get-ChildItem HKLM:\SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate -Verbose
Write-Host "Setting services to startup back to Automatic..."
'CryptSvc','wuauserv','bits' | % { Set-Service -Name $_ -StartupType Automatic -Verbose }
Write-Host "Starting back wu services..."
Start-Service -Name wuauserv -Verbose
Start-Service -Name cryptSvc -Verbose
Start-Service -Name bits -Verbose
Start-Service -Name msiserver -Verbose
Write-Host "Kick off a sup scan via cm client action. see windows\ccm\wuahandler.log"
Invoke-WMIMethod -Namespace root\ccm -Class SMS_CLIENT -Name TriggerSchedule "{00000000-0000-0000-0000-000000000113}"
Write-Host "Kick off a sup scan via cm client action. see windows\ccm\updatesdeployment.log"
Invoke-WMIMethod -Namespace root\ccm -Class SMS_CLIENT -Name TriggerSchedule "{00000000-0000-0000-0000-000000000108}"
Write-Host "Script execution completed."
Stop-Transcript
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment