Skip to content

Instantly share code, notes, and snippets.

@hemebond
Created March 2, 2015 10:27
Show Gist options
  • Save hemebond/29530d88f5753fd0b85c to your computer and use it in GitHub Desktop.
Save hemebond/29530d88f5753fd0b85c to your computer and use it in GitHub Desktop.
PowerShell script checks for available updates and returns the result in the standard monitoring plugin output format.
$Session = New-Object -ComObject "Microsoft.Update.Session"
$Searcher = $Session.CreateUpdateSearcher()
$Result = $Searcher.Search("IsInstalled = 0 and IsHidden = 0")
$Updates = $Result.Updates
$Total = $Updates.Count
$Critical = @($Updates | Where-Object {$_.MsrcSeverity -eq 'Important' -or $_.MsrcSeverity -eq 'Critical'}).Count
if ($Critical -gt 0) {
$CheckResult = 'CRITICAL'
$ExitCode = 2
}
elseif ($Total -gt 0 ) {
$CheckResult = 'WARNING'
$ExitCode = 1
}
else {
$CheckResult = 'OK'
$ExitCode = 0
}
Write-Output "$($CheckResult): $($Total) updates available ($($Critical) critical updates)."
Exit $ExitCode
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment