Skip to content

Commit

Permalink
crash when the windows update api returns an invalid update object
Browse files Browse the repository at this point in the history
  • Loading branch information
rgl committed Jul 12, 2024
1 parent 154314f commit c024b04
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 11 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Configure your packer template to require a [release version of the plugin](http
packer {
required_plugins {
windows-update = {
version = "0.16.4"
version = "0.16.5"
source = "github.com/rgl/windows-update"
}
}
Expand Down
1 change: 1 addition & 0 deletions test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export PACKER_LOG_PATH=test.log
export PACKER_CONFIG_DIR="$PWD/dist/test"
export PACKER_PLUGIN_PATH="$PWD/dist/test/plugins"
export PKR_VAR_disk_image=~/.vagrant.d/boxes/windows-2022-amd64/0.0.0/libvirt/box_0.img
#export PKR_VAR_disk_image=~/.vagrant.d/boxes/windows-11-23h2-amd64/0.0.0/libvirt/box_0.img

packer init -only=qemu.test test.pkr.hcl
packer build -only=qemu.test -on-error=abort test.pkr.hcl
28 changes: 18 additions & 10 deletions update/windows-update.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -187,17 +187,26 @@ while ($true) {
$rebootRequired = $false
for ($i = 0; $i -lt $searchResult.Updates.Count; ++$i) {
$update = $searchResult.Updates.Item($i)
if (!$update) {
continue
}

$updateTitle = $update.Title
if (!$updateTitle) {
continue
# crash when the windows update api returns an invalid update object.
# see https://github.com/rgl/packer-plugin-windows-update/issues/144
$expectedProperties = @(
'Title'
'MaxDownloadSize'
'LastDeploymentChangeTime'
'InstallationBehavior'
'AcceptEula'
)
$properties = $update `
| Get-Member $expectedProperties `
| Select-Object -ExpandProperty Name
if (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."
}

$updateMaxDownloadSize = try { [int64]$update.MaxDownloadSize } catch { [int64]0 }
$updateDate = try { $update.LastDeploymentChangeTime.ToString('yyyy-MM-dd') } catch { '1970-01-01' }
$updateTitle = $update.Title
$updateMaxDownloadSize = $update.MaxDownloadSize
$updateDate = $update.LastDeploymentChangeTime.ToString('yyyy-MM-dd')
$updateSize = ($updateMaxDownloadSize/1024/1024).ToString('0.##')
$updateSummary = "Windows update ($updateDate; $updateSize MB): $updateTitle"

Expand All @@ -206,8 +215,7 @@ for ($i = 0; $i -lt $searchResult.Updates.Count; ++$i) {
continue
}

$updateCanRequestUserInput = try { $update.InstallationBehavior.CanRequestUserInput } catch { $false }
if ($updateCanRequestUserInput) {
if ($update.InstallationBehavior.CanRequestUserInput) {
Write-Output "Warning The update '$updateTitle' has the CanRequestUserInput flag set (if the install hangs, you might need to exclude it with the filter 'exclude:`$_.InstallationBehavior.CanRequestUserInput' or 'exclude:`$_.Title -like '*$updateTitle*'')"
}

Expand Down

0 comments on commit c024b04

Please sign in to comment.