Files
management-scripts/Fix-WindowsUpdate.ps1
T

48 lines
1.9 KiB
PowerShell
Raw Normal View History

2020-08-18 16:20:20 -04:00
param([switch]$WSUS)
2020-10-22 14:01:35 -04:00
LogMsg "Fix-WindowsUpdate.ps1"
2020-08-18 16:20:20 -04:00
## Delete any existing folder from a previous execution of this script
2020-10-22 14:01:35 -04:00
If ( Test-Path "${Env:WinDir}\SoftwareDistribution.old" )
{
LogMsg "Deleting ""${Env:WinDir}\SoftwareDistribution.old"""
Try { Remove-Item "${Env:WinDir}\SoftwareDistribution.old" -Recurse -Force }
Catch { LogErr $_.Exception.Message }
}
2020-08-18 16:20:20 -04:00
## Stop Background Intelligent Transfer Services and Windows Update
2020-10-22 14:01:35 -04:00
LogMsg "Stopping BITS service"
Try { Stop-Service BITS }
Catch { LogErr $_.Exception.Message }
LogMsg "Stopping wuauserv service"
Try { Stop-Service wuauserv }
Catch { LogErr $_.Exception.Message }
2020-08-18 16:20:20 -04:00
## Rename the SoftwareDistribution folder, forcing Windows Update to recreate all metrics and redownload all updates
2020-10-22 14:01:35 -04:00
LogMsg "Renaming ""${Env:WinDir}\SoftwareDistribution"" to ""${Env:WinDir}\SoftwareDistribution.old"""
Try { Rename-Item -Path "${Env:WinDir}\SoftwareDistribution" -NewName "${Env:WinDir}\SoftwareDistribution.old" -Force }
Catch { LogErr $_.Exception.Message }
2020-08-18 16:20:20 -04:00
## Start Windows Update and Background Intelligent Transfer Services
2020-10-22 14:01:35 -04:00
LogMsg "Starting wuauserv service"
Try { Start-Service wuauserv }
Catch { LogErr $_.Exception.Message }
LogMsg "Starting BITS service"
Try { Start-Service BITS }
Catch { LogErr $_.Exception.Message }
2020-08-18 16:20:20 -04:00
## Delete SoftwareDistribution.old from this execution of this script
2020-10-22 14:01:35 -04:00
If ( Test-Path "${Env:WinDir}\SoftwareDistribution.old" )
{
LogMsg "Deleting ""${Env:WinDir}\SoftwareDistribution.old"""
Try { Remove-Item "${Env:WinDir}\SoftwareDistribution.old" -Recurse -Force }
Catch { LogErr $_.Exception.Message }
}
2020-08-18 16:20:20 -04:00
## If using WSUS, reset authorization with server and detect new updates
2020-10-22 14:01:35 -04:00
If ( $WSUS )
{
LogMsg "Resetting WSUS authorization and starting update detection"
Try { Start-Process "${Env:WinDir}\System32\wuauctl.exe" -ArgumentList "/resetauthorization /detectnow" }
Catch { LogErr $_.Exception.Message }
}