-
Notifications
You must be signed in to change notification settings - Fork 0
/
ForceWindowsUpdate_Claude.ps1
47 lines (37 loc) · 1.54 KB
/
ForceWindowsUpdate_Claude.ps1
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
# Force installation of pending Windows updates and reboot
# This script assumes updates are already downloaded and pending installation
# Create a log file
$logFile = "C:\Windows\Logs\ForceUpdateInstall.log"
Start-Transcript -Path $logFile -Append
try {
Write-Output "Starting forced installation of pending updates..."
# Use wuauclt to initiate the installation of pending updates
$wuaucltResult = wuauclt /detectnow /updatenow
Write-Output "wuauclt command executed. Result: $wuaucltResult"
# Wait for the update process to start and complete (adjust timeout as needed)
$timeout = 3600 # 1 hour timeout
$timer = [Diagnostics.Stopwatch]::StartNew()
while ($timer.Elapsed.TotalSeconds -lt $timeout) {
$updateSession = New-Object -ComObject Microsoft.Update.Session
$updateSearcher = $updateSession.CreateUpdateSearcher()
$pendingCount = $updateSearcher.GetTotalHistoryCount()
if ($pendingCount -eq 0) {
Write-Output "No more pending updates found. Installation likely completed."
break
}
Write-Output "Updates still in progress. Waiting..."
Start-Sleep -Seconds 60 # Check every minute
}
if ($timer.Elapsed.TotalSeconds -ge $timeout) {
Write-Output "Timeout reached. Update process may not have completed."
}
Write-Output "Update installation process finished or timed out. Initiating reboot..."
# Force a reboot
Restart-Computer -Force
}
catch {
Write-Output "An error occurred: $_"
}
finally {
Stop-Transcript
}