Document updating the agents, and harden the Windows update check
- AGENT_UPDATE.md: when an agent needs updating, how (reuse the stored token or rotate it), how to do many servers, how to check it worked, and why a server can stay 'not reported'. - Windows agent: the Windows Update search runs in its own process with a 150 s limit (the scheduled task is stopped after five minutes), a failed search is not retried for an hour, and the cache is read the same in PowerShell 7 and Windows PowerShell 5.1. - Linux agent: says in the journal when the package manager could not be read. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
171511c6db
commit
3da5c639b1
5 files changed
+225
-18
No files matched your search
@@ -232,36 +232,55 @@ function Test-RebootRequired {
|
||||
return $false
|
||||
}
|
||||
|
||||
# Asks Windows Update what is waiting. That goes over the network and can take a minute, so it is done at most every six hours and the
|
||||
# last answer is reused in between; whether a restart is due is cheap and always read fresh. Nothing is installed or downloaded.
|
||||
# Asks Windows Update what is waiting. That goes over the network and can take a minute or two, and the scheduled task is stopped
|
||||
# after five minutes, so the search runs in a separate process with a time limit, and is repeated at most every six hours; the last
|
||||
# answer is reused in between. A search that fails or runs out of time is not tried again for an hour, so a broken Windows Update
|
||||
# can't slow down every report. Whether a restart is due is cheap and always read fresh. Nothing is installed or downloaded.
|
||||
function Get-PendingUpdates {
|
||||
$result = $null
|
||||
$cached = $null
|
||||
try {
|
||||
if (Test-Path $script:UpdatesCachePath) {
|
||||
$cached = Get-Content -Raw -Path $script:UpdatesCachePath | ConvertFrom-Json
|
||||
$checked = [datetime]::Parse([string]$cached.checked_at, [Globalization.CultureInfo]::InvariantCulture, [Globalization.DateTimeStyles]::RoundtripKind)
|
||||
if (([datetime]::UtcNow - $checked.ToUniversalTime()).TotalHours -lt 6) { $result = $cached }
|
||||
# PowerShell 7 hands a stored date back as a date, Windows PowerShell 5.1 as the text it was written as.
|
||||
$checked = $cached.checked_at
|
||||
if ($checked -isnot [datetime]) { $checked = [datetime]::Parse([string]$checked, [Globalization.CultureInfo]::InvariantCulture, [Globalization.DateTimeStyles]::RoundtripKind) }
|
||||
$limitHours = 6
|
||||
if ($cached.failed) { $limitHours = 1 }
|
||||
if (([datetime]::UtcNow - $checked.ToUniversalTime()).TotalHours -ge $limitHours) { $cached = $null }
|
||||
}
|
||||
} catch { $result = $null }
|
||||
} catch { $cached = $null }
|
||||
|
||||
if (-not $result) {
|
||||
$session = New-Object -ComObject Microsoft.Update.Session
|
||||
$found = $session.CreateUpdateSearcher().Search("IsInstalled=0 and IsHidden=0 and Type='Software'")
|
||||
$security = 0
|
||||
foreach ($u in $found.Updates) {
|
||||
foreach ($c in $u.Categories) {
|
||||
if ($c.Name -eq 'Security Updates' -or $c.Name -eq 'Critical Updates') { $security++; break }
|
||||
if (-not $cached) {
|
||||
$job = Start-Job -ScriptBlock {
|
||||
$session = New-Object -ComObject Microsoft.Update.Session
|
||||
$found = $session.CreateUpdateSearcher().Search("IsInstalled=0 and IsHidden=0 and Type='Software'")
|
||||
$security = 0
|
||||
foreach ($u in $found.Updates) {
|
||||
foreach ($c in $u.Categories) {
|
||||
if ($c.Name -eq 'Security Updates' -or $c.Name -eq 'Critical Updates') { $security++; break }
|
||||
}
|
||||
}
|
||||
[pscustomobject]@{ count = [int]$found.Updates.Count; security = [int]$security }
|
||||
}
|
||||
$result = [pscustomobject][ordered]@{ checked_at = [datetime]::UtcNow.ToString('o'); count = [int]$found.Updates.Count; security = [int]$security }
|
||||
$answer = $null
|
||||
try {
|
||||
if (Wait-Job -Job $job -Timeout 150) { $answer = Receive-Job -Job $job -ErrorAction Stop } else { Stop-Job -Job $job }
|
||||
} catch { $answer = $null } finally { Remove-Job -Job $job -Force -ErrorAction SilentlyContinue }
|
||||
|
||||
$count = 0
|
||||
$security = 0
|
||||
if ($null -ne $answer) { $count = [int]$answer.count; $security = [int]$answer.security }
|
||||
$cached = [pscustomobject][ordered]@{ checked_at = [datetime]::UtcNow.ToString('o'); failed = ($null -eq $answer); count = $count; security = $security }
|
||||
try {
|
||||
$dir = Split-Path $script:UpdatesCachePath
|
||||
if (-not (Test-Path $dir)) { $null = New-Item -ItemType Directory -Path $dir -Force }
|
||||
ConvertTo-Json -InputObject $result | Set-Content -Path $script:UpdatesCachePath -Encoding UTF8
|
||||
ConvertTo-Json -InputObject $cached | Set-Content -Path $script:UpdatesCachePath -Encoding UTF8
|
||||
} catch { }
|
||||
}
|
||||
|
||||
return [ordered]@{ manager = 'windows-update'; count = [int]$result.count; security = [int]$result.security; reboot_required = (Test-RebootRequired) }
|
||||
# No answer is no answer: the report then leaves updates out, and the server keeps what it was last told.
|
||||
if ($cached.failed) { return $null }
|
||||
return [ordered]@{ manager = 'windows-update'; count = [int]$cached.count; security = [int]$cached.security; reboot_required = (Test-RebootRequired) }
|
||||
}
|
||||
|
||||
function Get-SystemInfo {
|
||||
|
||||
Reference in new issue
Block a user