Add Windows agent install updates polling

This commit is contained in:
2026-10-13 18:50:48 +00:00
parent fe689f4cca
commit b6804002f6
@@ -76,6 +76,42 @@ if ($PollForCommands -and $InventoryApiUrl -and $InventoryApiToken) {
$inventoryPatch | Add-Member -NotePropertyName commandAction -NotePropertyValue 'reboot' -Force
$inventoryPatch | Add-Member -NotePropertyName commandId -NotePropertyValue $command.id -Force
}
elseif ($command -and $command.commandAvailable -and $command.action -eq 'install_updates') {
$startResult = [ordered]@{
hostname = $inventoryPatch.hostname
id = $command.id
action = $command.action
status = 'installing'
receivedAtUtc = (Get-Date).ToUniversalTime().ToString('o')
reason = $command.reason
}
Send-WpmPatchCommandResult -StatusApiUrl $InventoryApiUrl -Result ([pscustomobject]$startResult) -ApiToken $InventoryApiToken | Out-Null
$installScript = Join-Path $PSScriptRoot 'Invoke-WindowsPatchInstall.ps1'
if (-not (Test-Path $installScript)) { throw "Install script not found: $installScript" }
$runFile = Join-Path (Split-Path -Parent $OutFile) 'last-install-command.json'
$installOutput = & $installScript -InventoryApiUrl $InventoryApiUrl -InventoryApiToken $InventoryApiToken -OutFile $runFile -Force -IgnoreReboot 2>&1
$exitCode = if ($LASTEXITCODE -ne $null) { [int]$LASTEXITCODE } else { 0 }
$installRun = $null
if (Test-Path $runFile) {
try { $installRun = Get-Content -Path $runFile -Raw | ConvertFrom-Json } catch { $installRun = $null }
}
$finalStatus = if ($exitCode -eq 0 -and $installRun -and $installRun.status -notin @('failed')) { 'completed' } elseif ($exitCode -eq 0) { 'completed' } else { 'failed' }
$result = [ordered]@{
hostname = $inventoryPatch.hostname
id = $command.id
action = $command.action
status = $finalStatus
receivedAtUtc = $startResult.receivedAtUtc
completedAtUtc = (Get-Date).ToUniversalTime().ToString('o')
exitCode = $exitCode
reason = $command.reason
installRun = $installRun
output = @($installOutput | ForEach-Object { [string]$_ })
}
Send-WpmPatchCommandResult -StatusApiUrl $InventoryApiUrl -Result ([pscustomobject]$result) -ApiToken $InventoryApiToken | Out-Null
$inventoryPatch | Add-Member -NotePropertyName commandAction -NotePropertyValue 'install_updates' -Force
$inventoryPatch | Add-Member -NotePropertyName commandId -NotePropertyValue $command.id -Force
}
}
catch {
$inventoryPatch | Add-Member -NotePropertyName commandError -NotePropertyValue $_.Exception.Message -Force