r/PowerShell • u/kylemerchant4 • Oct 11 '26
Script Sharing I wrote a patch status script that exits non-zero when it can't check something
Most patch/reboot check scripts I've used print "OK" even when a check silently failed (no rights to WMI, registry key missing, WU agent broken). So I wrote one where the exit code tells the truth:
- 0: every check ran
- 2: PARTIAL, at least one check couldn't run, with the reason listed
- 1: it couldn't write the report
It checks last installed update (flags it if older than N days), pending reboot, uptime, and optionally lists waiting updates through the WU agent. JSON and CSV output.
Every check runs in its own try/catch. A failure doesn't stop the script or get swallowed; it gets recorded:
function Add-CheckNotRun {
param([string]$Check, [string]$Reason)
$script:ChecksNotRun += [PSCustomObject]@{ Check = $Check; Result = 'check could not run'; Reason = $Reason }
Write-Host " -> CHECK COULD NOT RUN: ${Check}: $Reason" -ForegroundColor Red
}Pending reboot looks at three sources:
$reasons = @()
if (Test-Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Component Based Servicing\RebootPending') {
$reasons += 'Component Based Servicing'
}
if (Test-Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\RebootRequired') {
$reasons += 'Windows Update'
}
$sm = Get-ItemProperty 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager'
if ($sm.PSObject.Properties['PendingFileRenameOperations'] -and $sm.PendingFileRenameOperations) {
$reasons += 'Pending file rename operations'
}And the end of the script decides the exit code:
if (@($script:ChecksNotRun).Count -gt 0) {
Write-Host " PARTIAL: $(@($script:ChecksNotRun).Count) check(s) could not run." -ForegroundColor Red
foreach ($c in $script:ChecksNotRun) { Write-Host " - $($c.Check): $($c.Reason)" -ForegroundColor Red }
exit 2
}
exit 0Full script (MIT): https://github.com/Dopelogik/windows-patch-status-report
Feedback welcome, especially on the reboot detection. I know PendingFileRenameOperations can throw false positives from some AV and installers, so I'm curious how others filter it.
3
u/g3n3 Oct 11 '26
The exit design is poor for powershell. Output actual object with a status.
2
u/purplemonkeymad Oct 12 '26
If you are using exit codes for a status, I would suggest to use the same set as nagios, that way your script is more likely easy to integrate with other solutions:
|0| OK| UP| |1| WARNING| UP or DOWN/UNREACHABLE*| |2| CRITICAL| DOWN/UNREACHABLE| |3| UNKNOWN| DOWN/UNREACHABLE|
So use 1 for ok with warnings (say pending reboot,) 2 for failed, 3 for unable to check.
1
u/BlackV Oct 11 '26 edited Oct 11 '26
Look at
that is inefficient design
as is this, plus you are messing with scopes, that could get messy
Whats this for
$reportis already an ordered object , why cast it ?additionally you then go and create a new pcsustom
why is that? again you already have it in
$reportwhy spit out a new object?what's the goal with
exit 0/1/2where are you using this ? intune as a remediation script ?
2
u/g3n3 Oct 11 '26
Powershell isn’t the typical shell so the whole exit bash-ism or cmd-ism isn’t ideal.
2
u/ankokudaishogun Oct 12 '26
I don't think it's a level or precision relevant to your specific script, but
$Report.UptimeDays = [math]::Round(((Get-Date) - $os.LastBootUpTime).TotalDays, 1)made me wonder if daylight saving changes impact calculation between DateTime values.That said, it would be a simple matter of adding two
.ToUniversalTime()