Skip to content

Commit

Permalink
try to repair the windows update settings to work in non-preview mode
Browse files Browse the repository at this point in the history
  • Loading branch information
rgl committed Jul 13, 2024
1 parent a9db6ec commit 2630608
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ Configure your packer template to require a [release version of the plugin](http
packer {
required_plugins {
windows-update = {
version = "0.16.5"
source = "github.com/rgl/windows-update"
version = "0.16.7"
source = "github.com/rgl/windows-update"
}
}
}
Expand Down
28 changes: 26 additions & 2 deletions update/windows-update.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,29 @@ function ExitWhenRebootRequired($rebootRequired = $false) {
}
}

# try to repair the windows update settings to work in non-preview mode.
# see https://github.com/rgl/packer-plugin-windows-update/issues/144
# see https://learn.microsoft.com/en-sg/answers/questions/1791668/powershell-command-outputting-system-comobject-on
function Repair-WindowsUpdate {
$settingsPath = 'C:\ProgramData\Microsoft\Windows\OneSettings\UusSettings.json'
if (!(Test-Path $settingsPath)) {
throw 'the windows update api is in an invalid state. see https://github.com/rgl/packer-plugin-windows-update/issues/144.'
}
$version = (New-Object -ComObject Microsoft.Update.AgentInfo).GetInfo('ProductVersionString')
$settings = Get-Content -Raw $settingsPath | ConvertFrom-Json
if ($settings.settings.EXCLUSIONS -notcontains $version) {
$settings.settings.EXCLUSIONS += $version
Write-Output 'Repairing the windows update settings to work in non-preview mode...'
Copy-Item $settingsPath "$settingsPath.backup.json" -Force
[System.IO.File]::WriteAllText(
$settingsPath,
($settings | ConvertTo-Json -Compress -Depth 100),
(New-Object System.Text.UTF8Encoding $false))
}
Write-Output 'Restarting the machine to retry a new windows update round...'
ExitWithCode 101
}

ExitWhenRebootRequired

if ($OnlyCheckForRebootRequired) {
Expand Down Expand Up @@ -188,7 +211,8 @@ $rebootRequired = $false
for ($i = 0; $i -lt $searchResult.Updates.Count; ++$i) {
$update = $searchResult.Updates.Item($i)

# crash when the windows update api returns an invalid update object.
# when the windows update api returns an invalid update object, repair
# windows update and signal a reboot to try again.
# see https://github.com/rgl/packer-plugin-windows-update/issues/144
$expectedProperties = @(
'Title'
Expand All @@ -201,7 +225,7 @@ for ($i = 0; $i -lt $searchResult.Updates.Count; ++$i) {
| Get-Member $expectedProperties `
| Select-Object -ExpandProperty Name
if (!$properties -or (Compare-Object $expectedProperties $properties)) {
throw "the windows update api returned a invalid update object. see https://github.com/rgl/packer-plugin-windows-update/issues/144."
Repair-WindowsUpdate
}

$updateTitle = $update.Title
Expand Down

0 comments on commit 2630608

Please sign in to comment.