Skip to content

Commit 463c46a

Browse files
committed
reboot after each update batch (to make sure that all the available windows updates are applied)
1 parent 0f02e21 commit 463c46a

1 file changed

Lines changed: 17 additions & 9 deletions

File tree

update/windows-update.ps1

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -84,16 +84,18 @@ function Wait-Condition {
8484
}
8585
}
8686

87-
function ExitWhenRebootRequired {
87+
function ExitWhenRebootRequired($rebootRequired = $false) {
8888
# check for pending Windows Updates.
89-
$systemInformation = New-Object -ComObject 'Microsoft.Update.SystemInfo'
90-
$rebootRequired = $systemInformation.RebootRequired
89+
if (!$rebootRequired) {
90+
$systemInformation = New-Object -ComObject 'Microsoft.Update.SystemInfo'
91+
$rebootRequired = $systemInformation.RebootRequired
92+
}
9193

9294
# check for pending Windows Features.
9395
if (!$rebootRequired) {
9496
$pendingPackagesKey = 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Component Based Servicing\PackagesPending'
9597
$pendingPackagesCount = (Get-ChildItem -ErrorAction SilentlyContinue $pendingPackagesKey | Measure-Object).Count
96-
$rebootRequired = $rebootRequired -or $pendingPackagesCount -gt 0
98+
$rebootRequired = $pendingPackagesCount -gt 0
9799
}
98100

99101
if ($rebootRequired) {
@@ -114,9 +116,11 @@ $updateSearcher = $updateSession.CreateUpdateSearcher()
114116
$updatesToDownload = New-Object -ComObject 'Microsoft.Update.UpdateColl'
115117
$searchResult = $updateSearcher.Search("IsInstalled=0 and Type='Software' and IsHidden=0")
116118
if ($searchResult.Updates.Count) {
119+
$rebootRequired = $false
120+
117121
for ($i = 0; $i -lt $searchResult.Updates.Count; ++$i) {
118122
$update = $searchResult.Updates.Item($i)
119-
123+
120124
if ($update.InstallationBehavior.CanRequestUserInput) {
121125
continue
122126
}
@@ -132,8 +136,9 @@ if ($searchResult.Updates.Count) {
132136
}
133137

134138
$updatesToDownload.Add($update) | Out-Null
135-
139+
136140
if ($updatesToDownload.Count -ge $UpdateLimit) {
141+
$rebootRequired = $true
137142
break
138143
}
139144
}
@@ -150,6 +155,11 @@ if ($searchResult.Updates.Count) {
150155
$update = $searchResult.Updates.Item($i)
151156
if ($update.IsDownloaded) {
152157
$updatesToInstall.Add($update) | Out-Null
158+
159+
if ($updatesToInstall.Count -ge $UpdateLimit) {
160+
$rebootRequired = $true
161+
break
162+
}
153163
}
154164
}
155165

@@ -158,9 +168,7 @@ if ($searchResult.Updates.Count) {
158168
$updateInstaller = $updateSession.CreateUpdateInstaller()
159169
$updateInstaller.Updates = $updatesToInstall
160170
$installResult = $updateInstaller.Install()
161-
if ($installResult.RebootRequired) {
162-
ExitWhenRebootRequired
163-
}
171+
ExitWhenRebootRequired ($installResult.RebootRequired -or $rebootRequired)
164172
}
165173
} else {
166174
Write-Output 'No Windows updates found'

0 commit comments

Comments
 (0)