Update Windows patch agent command polling

This commit is contained in:
2026-10-13 02:39:41 +00:00
parent ab9f8b7c63
commit bfab394c17
3 changed files with 68 additions and 6 deletions
@@ -199,6 +199,36 @@ function Send-WpmPatchStatus {
Invoke-RestMethod -Uri $ApiUrl -Method Post -ContentType 'application/json' -Headers $headers -Body $json
}
function Get-WpmPatchCommand {
[CmdletBinding()]
param(
[Parameter(Mandatory)][string]$StatusApiUrl,
[Parameter(Mandatory)][string]$Hostname,
[string]$ApiToken
)
$headers = @{}
if ($ApiToken) { $headers['Authorization'] = "Bearer $ApiToken" }
$base = $StatusApiUrl -replace '/status$', ''
$safeHost = [uri]::EscapeDataString($Hostname)
Invoke-RestMethod -Uri "$base/command/$safeHost" -Method Get -Headers $headers
}
function Send-WpmPatchCommandResult {
[CmdletBinding()]
param(
[Parameter(Mandatory)][string]$StatusApiUrl,
[Parameter(Mandatory)][object]$Result,
[string]$ApiToken
)
$headers = @{}
if ($ApiToken) { $headers['Authorization'] = "Bearer $ApiToken" }
$base = $StatusApiUrl -replace '/status$', ''
$json = $Result | ConvertTo-Json -Depth 12
Invoke-RestMethod -Uri "$base/command-result" -Method Post -ContentType 'application/json' -Headers $headers -Body $json
}
function Invoke-WpmWindowsUpdateInstall {
[CmdletBinding(SupportsShouldProcess)]
param(
@@ -245,4 +275,4 @@ function Invoke-WpmWindowsUpdateInstall {
}
}
Export-ModuleMember -Function Get-WpmPendingReboot, Get-WpmWindowsUpdateStatus, Test-WpmPatchWindow, Send-WpmPatchStatus, Invoke-WpmWindowsUpdateInstall
Export-ModuleMember -Function Get-WpmPendingReboot, Get-WpmWindowsUpdateStatus, Test-WpmPatchWindow, Send-WpmPatchStatus, Get-WpmPatchCommand, Send-WpmPatchCommandResult, Invoke-WpmWindowsUpdateInstall
+28 -1
View File
@@ -4,7 +4,8 @@ param(
[string]$InventoryApiToken,
[string]$OutFile = "$env:ProgramData\WheelzPatch\status.json",
[switch]$IncludeDrivers,
[switch]$IncludeInstalledHistory
[switch]$IncludeInstalledHistory,
[switch]$PollForCommands
)
$ErrorActionPreference = 'Stop'
@@ -21,6 +22,7 @@ if (Test-Path $configPath) {
$config = Get-Content -Path $configPath -Raw | ConvertFrom-Json
if (-not $InventoryApiUrl -and $config.inventoryApiUrl) { $InventoryApiUrl = $config.inventoryApiUrl }
if (-not $InventoryApiToken -and $config.inventoryApiToken) { $InventoryApiToken = $config.inventoryApiToken }
if (-not $PollForCommands -and $config.pollForCommands) { $PollForCommands = [bool]$config.pollForCommands }
}
catch {
Write-Warning "Failed to read WheelzPatch config.json: $($_.Exception.Message)"
@@ -55,4 +57,29 @@ if ($InventoryApiUrl) {
Send-WpmPatchStatus -Status $inventoryPatch -ApiUrl $InventoryApiUrl -ApiToken $InventoryApiToken | Out-Null
}
if ($PollForCommands -and $InventoryApiUrl -and $InventoryApiToken) {
try {
$command = Get-WpmPatchCommand -StatusApiUrl $InventoryApiUrl -Hostname $inventoryPatch.hostname -ApiToken $InventoryApiToken
if ($command -and $command.commandAvailable -and $command.action -eq 'reboot') {
$result = [ordered]@{
hostname = $inventoryPatch.hostname
id = $command.id
action = $command.action
status = 'rebooting'
receivedAtUtc = (Get-Date).ToUniversalTime().ToString('o')
reason = $command.reason
}
Send-WpmPatchCommandResult -StatusApiUrl $InventoryApiUrl -Result ([pscustomobject]$result) -ApiToken $InventoryApiToken | Out-Null
$rebootScript = Join-Path $PSScriptRoot 'Invoke-WindowsPatchReboot.ps1'
if (-not (Test-Path $rebootScript)) { throw "Reboot script not found: $rebootScript" }
& $rebootScript -Reason $command.reason -Force
$inventoryPatch | Add-Member -NotePropertyName commandAction -NotePropertyValue 'reboot' -Force
$inventoryPatch | Add-Member -NotePropertyName commandId -NotePropertyValue $command.id -Force
}
}
catch {
$inventoryPatch | Add-Member -NotePropertyName commandError -NotePropertyValue $_.Exception.Message -Force
}
}
$inventoryPatch | ConvertTo-Json -Depth 16
@@ -5,10 +5,11 @@ param(
[string]$InventoryApiUrl = 'https://psu.local.wheelz.com/api/wheelz/patch/windows/status',
[string]$EnrollmentToken,
[string]$InventoryApiToken,
[string]$StatusSchedule = 'Every 4 hours',
[string]$StatusSchedule = 'Every 15 minutes',
[string]$PatchSchedule = 'Wednesday after Patch Tuesday · 2:00 AM CT',
[switch]$EnablePatchTask,
[switch]$LocalOnly,
[switch]$PollForCommands = $true,
[switch]$Force
)
@@ -49,7 +50,8 @@ $config = [ordered]@{
inventoryApiUrl = $InventoryApiUrl
inventoryApiToken = $EnrollmentToken
patchWindow = $PatchSchedule
rebootPolicyEnabled = $false
rebootPolicyEnabled = [bool]$PollForCommands
pollForCommands = [bool]$PollForCommands
installedAtUtc = (Get-Date).ToUniversalTime().ToString('o')
}
$configPath = Join-Path $InstallRoot 'config.json'
@@ -58,9 +60,10 @@ $config | ConvertTo-Json -Depth 8 | Set-Content -Path $configPath -Encoding UTF8
$statusArgs = @('-NoProfile','-ExecutionPolicy','Bypass','-File',"$scriptsDir\Get-WindowsPatchStatus.ps1")
if ($InventoryApiUrl) { $statusArgs += @('-InventoryApiUrl',$InventoryApiUrl) }
if ($InventoryApiToken) { $statusArgs += @('-InventoryApiToken',$EnrollmentToken) }
if ($PollForCommands) { $statusArgs += @('-PollForCommands') }
$statusArgument = (($statusArgs | ForEach-Object { if ($_ -match '\s') { '"{0}"' -f $_ } else { $_ } }) -join ' ')
$statusAction = New-ScheduledTaskAction -Execute 'powershell.exe' -Argument $statusArgument
$statusTrigger = New-ScheduledTaskTrigger -Once -At (Get-Date).AddMinutes(5) -RepetitionInterval (New-TimeSpan -Hours 4) -RepetitionDuration (New-TimeSpan -Days 3650)
$statusTrigger = New-ScheduledTaskTrigger -Once -At (Get-Date).AddMinutes(5) -RepetitionInterval (New-TimeSpan -Minutes 15) -RepetitionDuration (New-TimeSpan -Days 3650)
$statusPrincipal = New-ScheduledTaskPrincipal -UserId 'SYSTEM' -RunLevel Highest
Register-ScheduledTask -TaskName 'Wheelz Windows Patch Status' -Action $statusAction -Trigger $statusTrigger -Principal $statusPrincipal -Force | Out-Null
@@ -74,7 +77,9 @@ if ($EnablePatchTask) {
Register-ScheduledTask -TaskName 'Wheelz Windows Patch Install' -Action $patchAction -Trigger $patchTrigger -Principal $statusPrincipal -Force | Out-Null
}
& "$scriptsDir\Get-WindowsPatchStatus.ps1" -InventoryApiUrl $InventoryApiUrl -InventoryApiToken $EnrollmentToken | Out-Host
$initialArgs = @('-InventoryApiUrl',$InventoryApiUrl,'-InventoryApiToken',$EnrollmentToken)
if ($PollForCommands) { $initialArgs += '-PollForCommands' }
& "$scriptsDir\Get-WindowsPatchStatus.ps1" @initialArgs | Out-Host
Write-Host "Wheelz Windows Patch agent installed at $InstallRoot"
Write-Host 'Status task: Wheelz Windows Patch Status'
if ($EnablePatchTask) { Write-Host 'Patch task: Wheelz Windows Patch Install' } else { Write-Host 'Patch task not enabled yet; rerun with -EnablePatchTask after status reporting is trusted.' }