Skip to content

Instantly share code, notes, and snippets.

@dotps1
Last active January 5, 2024 08:22
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dotps1/8abb564c6dbfb1b768354b39ede033da to your computer and use it in GitHub Desktop.
Save dotps1/8abb564c6dbfb1b768354b39ede033da to your computer and use it in GitHub Desktop.
Resets the Windows Update Agent components.
<#
.SYNOPSIS
Resets the requried components of the Windows Update Agent.
.DESCRIPTION
Stops required services.
Removes cached files/folders used by the Windows Update Agent.
Resets the permissions on the bits and wuauserv services.
Registers dlls needed by the bits/Windows Update Agent.
Removes lingering registry entries from SUS if present.
Restarts all the services that where stopped.
Resets the bits cache.
.NOTES
This is an 'invasive' reset, meaning it resets/removes everything, with the exception of the WindowsUpdate.log.
Release Notes:
20160707 - Initial Release.
20160708 - Added logic to search and remove ANY folder in $env:SystemRoot that are named SoftwareDistribution*.
20160817 - Added the Exception.Message to the error so the failure reason can be determined.
20160818 - Changed the SoftwareDistribution removal code to use a foreach item method, so even if a file/folder is locked, the deletion does not give up.
20160822 - Changed the registering of dlls from using Start-Process to just calling the regsrv32.exe directly.
#>
<# Verify the Windows Update Agent is up to date.
try {
if ([Version](Get-Item -Path "$env:SystemRoot\System32\Wuaueng.dll" -ErrorAction Stop).VersionInfo.ProductVersion -lt [Version]'7.6.7601.19161') {
Write-Error -Message 'The Windows Update Agent is out of date.'
exit 1
}
} catch {
$_
exit 1
}#>
# Stop and disable services.
$services = @(
'bits',
'wuauserv',
'appidsvc',
'cryptsvc'
)
foreach ($service in $services) {
try {
if ($s = Get-Service -Name $service -ErrorAction 'Stop') {
if ($s.Status -eq 'Running') {
try {
Write-Output -InputObject "Stopping service: '$service'."
Stop-Service -Name $service -Force -ErrorAction 'Stop'
Write-Output -InputObject "Stopped service: '$service'."
} catch {
Write-Error -Message "Failed to stop service: '$service'. $($_.Exception.Message)"
$_
exit 1
}
} else {
Write-Output -InputObject "Service is already stopped: '$service'."
}
}
} catch {
Write-Warning -Message "Service does not exsist: '$service'."
}
}
# Remove qmgr[Int].dat files.
try {
foreach ($item in (Get-ChildItem -Path "$env:ProgramData\Application Data\Microsoft\Network\Downloader" -Filter 'qmgr*.dat' -ErrorAction 'Stop')) {
try {
Write-Output -InputObject "Removing item: '$($item.FullName)'."
Remove-Item -Path $item.FullName -Force -Confirm:$false -ErrorAction 'Stop'
Write-Output -InputObject "Removed item: '$($item.FullName)'. $($_.Exception.Message)"
} catch {
Write-Error -Message "Failed to delete file: '$($item.FullName)'."
}
}
} catch {
$_
}
# Remove SoftwareDistribution folder contents.
if (Test-Path -Path "$env:SystemRoot\SoftwareDistribution") {
Write-Output -InputObject 'Removing SoftwareDistribution folder contents.'
foreach ($item in (Get-ChildItem -Path $env:SystemRoot\SoftwareDistribution -Recurse | Sort-Object -Property FullName -Descending)) {
try {
Remove-Item -Path $item.FullName -Confirm:$false -Force -ErrorAction Stop
} catch {
Write-Error -Message "Failed to remove '$($item.FullName)'. $($_.Exception.Message)"
continue
}
}
Write-Output -InputObject "Removed SoftwareDistribution contents."
} else {
Write-Warning -Message "Cound not find part of path: '$env:SystemRoot\SoftwareDistribution'."
}
# Cleanup possiable old renamed SoftwareDistribution folders.
foreach ($folder in (Get-Item -Path $env:SystemRoot\SoftwareDistribution* | Where-Object -FilterScript { $_.Name -ne 'SoftwareDistribution' })) {
Write-Output -InputObject 'Found renamed SoftwareDistribution folder(s). Attempting to remove them.'
try {
Write-Output -InputObject "Removing '$folder'."
Remove-Item -Path $folder.FullName -Recurse -Confirm:$false -Force -ErrorAction Stop
Write-Output -InputObject "Removed '$folder'. $($_.Exception.Message)"
} catch {
Write-Error -Message "Failed to remove '$folder'."
}
}
# Remove CatRoot2 folder contents.
if (Test-Path -Path "$env:SystemRoot\System32\CatRoot2") {
try {
Write-Output -InputObject 'Removing CatRoot2 folder contents.'
Remove-Item -Path "$env:SystemRoot\System32\CatRoot2" -Filter * -Recurse -Confirm:$false -ErrorAction 'Stop'
Write-Output -InputObject 'Removed CatRoot2 folder contents.'
} catch {
Write-Error -Message "Failed to remove CatRoot2 folder contents. $($_.Exception.Message)"
}
} else {
Write-Warning -Message "Count not find part of path: '$env:SystemRoot\System32\CatRoot2'"
}
# Reset permissions on bits service.
try {
Write-Output -InputObject 'Reseting bits service permissions.'
Start-Process -FilePath 'sc.exe' -ArgumentList 'sdset', 'bits', 'D:(A;;CCLCSWRPWPDTLOCRRC;;;SY)(A;;CCDCLCSWRPWPDTLOCRSDRCWDWO;;;BA)(A;;CCLCSWLOCRRC;;;AU)(A;;CCLCSWRPWPDTLOCRRC;;;PU)' -ErrorAction 'Stop' |
Out-Null
Write-Output -InputObject 'Reset bits service permissions.'
} catch {
Write-Error -Message "Failed to reset bits service permissions. $($_.Exception.Message)"
}
# Reset permissions on wuauserv services.
try {
Write-Output -InputObject 'Reseting wuauserv service permissions.'
Start-Process -FilePath 'sc.exe' -ArgumentList 'sdset', 'wuauserv', 'D:(A;;CCLCSWRPWPDTLOCRRC;;;SY)(A;;CCDCLCSWRPWPDTLOCRSDRCWDWO;;;BA)(A;;CCLCSWLOCRRC;;;AU)(A;;CCLCSWRPWPDTLOCRRC;;;PU)' -ErrorAction 'Stop' |
Out-Null
Write-Output -InputObject 'Reset wuauserv service permissions.'
} catch {
Write-Error -Message "Failed to reset bwuauserv service permissions. $($_.Exception.Message)"
}
# Regester dlls.
$dlls = @(
'atl.dll',
'urlmon.dll',
'mshtml.dll',
'shdocvw.dll',
'browseui.dll',
'jscript.dll',
'vbscript.dll',
'scrrun.dll',
'msxml.dll',
'msxml3.dll',
'msxml6.dll',
'actxprxy.dll',
'softpub.dll',
'wintrust.dll',
'dssenh.dll',
'rsaenh.dll',
'gpkcsp.dll',
'sccbase.dll',
'slbcsp.dll',
'cryptdlg.dll',
'oleaut32.dll',
'ole32.dll',
'shell32.dll',
'initpki.dll',
'wuapi.dll',
'wuaueng.dll',
'wuaueng1.dll',
'wucltui.dll',
'wups.dll',
'wups2.dll',
'wuweb.dll',
'qmgr.dll',
'qmgrprxy.dll',
'wucltux.dll',
'muweb.dll',
'wuwebv.dll'
)
foreach ($dll in $dlls) {
try {
Write-Output -InputObject "Regestering dll: '$dll'."
regsvr32.exe /s $dll
Write-Output -InputObject "Regestered dll: '$dll'."
} catch {
Write-Error -Message "Failed to register dll: '$dll'. $($_.Exception.Message)"
}
}
# Delete Registry Key Properties.
$properties = @(
'AccountDomainSid',
'PingID',
'SusClientId'
)
foreach ($property in $properties) {
try {
if ($item = Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate" -Name $property -ErrorAction 'Stop') {
try {
Write-Output -InputObject "Removing registry key property: '$property'."
Remove-ItemProperty -Path $item.PSPath -Name $property -Force -Confirm:$false -ErrorAction 'Stop'
Write-Output -InputObject "Removed registry key property: '$property'."
} catch {
Write-Error -Message "Failed to remove registry key property: '$property'. $($_.Exception.Message)"
}
}
} catch {
Write-Warning -Message "Registy key property: '$property' does not exist."
}
}
# Restart and enable services.
foreach ($service in $services) {
try {
if ($s = Get-Service -Name $service -ErrorAction 'Stop') {
if ($s.Status -ne 'Running') {
try {
Write-Output -InputObject "Starting service: '$service'."
Start-Service -Name $service -ErrorAction 'Stop'
Write-Output -InputObject "Started service: '$service'."
} catch {
Write-Error -Message "Failed to start service: '$service'. $($_.Exception.Message)"
$_
}
}
}
} catch {
Write-Warning -Message "Service does not exsist: '$service'."
}
}
# Reset the bits the jobs.
try {
Write-Output -InputObject 'Reseting the bits jobs for all users.'
Start-Process -FilePath 'bitsadmin.exe' -ArgumentList '/reset', '/allusers' -ErrorAction 'Stop' |
Out-Null
Write-Output -InputObject 'Reset the bits jobs for all users.'
} catch {
Write-Error -Message 'Failed to reset the bits jobs for all users.'
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment