Repository navigation
Expand file tree
/
Copy pathRepair-Windows.ps1
More file actions
72 lines (63 loc) · 2.55 KB
/
Copy pathRepair-Windows.ps1
File metadata and controls
72 lines (63 loc) · 2.55 KB
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
param(
[switch]$SkipDism,
[switch]$SkipSfc,
[switch]$ResetWindowsUpdate,
[switch]$FlushDns
)
$ErrorActionPreference = 'Stop'
$baseUrl = if ($env:WINUPDATE_BASEURL) { $env:WINUPDATE_BASEURL.TrimEnd('/') } else { 'https://raw.githubusercontent.com/TheTrueZeroTwo/winupdate/main' }
$commonCode = (New-Object Net.WebClient).DownloadString("$baseUrl/Common.ps1")
. ([ScriptBlock]::Create($commonCode))
$log = Start-WumLog -Name 'repair-windows'
try {
Assert-WumAdmin
Write-WumSection 'Windows component repair'
if (-not $SkipDism) {
Write-WumStep 'Running DISM component store restorehealth'
Invoke-WumNativeCommand -FilePath 'dism.exe' -Arguments @('/Online','/Cleanup-Image','/RestoreHealth') -IgnoreExitCode | Out-Null
}
if (-not $SkipSfc) {
Write-WumStep 'Running system file checker'
Invoke-WumNativeCommand -FilePath 'sfc.exe' -Arguments @('/scannow') -IgnoreExitCode | Out-Null
}
if ($ResetWindowsUpdate) {
Write-WumSection 'Reset Windows Update components'
$services = @('bits','wuauserv','appidsvc','cryptsvc')
foreach ($svc in $services) {
try {
Write-WumStep "Stopping $svc"
Stop-Service -Name $svc -Force -ErrorAction SilentlyContinue
} catch { }
}
$sd = Join-Path $env:SystemRoot 'SoftwareDistribution'
$cr = Join-Path $env:SystemRoot 'System32\catroot2'
$stamp = Get-Date -Format 'yyyyMMdd-HHmmss'
foreach ($path in @($sd,$cr)) {
if (Test-Path $path) {
$newName = "$(Split-Path $path -Leaf).bak-$stamp"
try {
Write-WumStep "Renaming $path to $newName"
Rename-Item -Path $path -NewName $newName -ErrorAction Stop
} catch {
Write-WumWarn "Could not rename ${path}: $($_.Exception.Message)"
}
}
}
foreach ($svc in $services) {
try {
Write-WumStep "Starting $svc"
Start-Service -Name $svc -ErrorAction SilentlyContinue
} catch { }
}
} else {
Write-WumWarn 'Windows Update component reset was skipped. Re-run with -ResetWindowsUpdate if needed.'
}
if ($FlushDns) {
Write-WumStep 'Flushing DNS cache'
Invoke-WumNativeCommand -FilePath 'ipconfig.exe' -Arguments @('/flushdns') -IgnoreExitCode | Out-Null
}
Write-WumSection 'Repair complete'
Write-Host "Pending reboot: $(Test-WumPendingReboot)"
} finally {
Stop-WumLog
}