Search Exchange
Search All Sites
Nagios Live Webinars
Let our experts show you how Nagios can help your organization.Login
Directory Tree
check_win_last_update.ps1
Meet The New Nagios Core Services Platform
Built on over 25 years of monitoring experience, the Nagios Core Services Platform provides insightful monitoring dashboards, time-saving monitoring wizards, and unmatched ease of use. Use it for free indefinitely.
Monitoring Made Magically Better
- Nagios Core on Overdrive
- Powerful Monitoring Dashboards
- Time-Saving Configuration Wizards
- Open Source Powered Monitoring On Steroids
- And So Much More!
which is understandable for every Manager :-}
should be called via NRPE/NSClient++
inspired by bratislav.stojkovic@gmail.com check_win_lastupdate.vbs
but with using Microsoft.Update.Session instead of Registry.
Registry query not working in W2016 or W10.
##################################################################################
#
# NAME: check_win_last_update.ps1
#
# AUTHOR: Peter Luetke-Bexten
# COAUTHOR: Christoph Hamschmidt
# EMAIL: peter.luetke-bexten (at) benteler.com
#
# COMMENT:
# Script to calculate the Days since last Windows Update
# which is understandable for every Manager :-}
# should be called via NRPE/NSClient++
# inspired by bratislav.stojkovic@gmail.com check_win_lastupdate.vbs
# but with using Microsoft.Update.Session instead of Registry.
# Registry query not working in W2016 or W10.
#
# NSCLIENT.ini:
# check_win_lastupdate = cmd /c echo scriptspowershellcheck_win_last_update.ps1 $ARG1$ $ARG2$ ; exit($lastexitcode) | "C:WindowsSystem32WindowsPowerShellv1.0powershell.exe" -noninteractive -noprofile -ExecutionPolicy unrestricted -command -
#
# NRPE Check:
# check_nrpe -H targethost -p 5666 -t 120 -u -c check_win_lastupdate -a 35 70
#
# CHANGELOG:
# 1.0 20180126 - initial version
#
################################################################################
# Script calculates curent date and date of last Windows Update Installation
## Arguments
$wldays = $args[0]
$cldays = $args[1]
## ReturnStates
$returnStateOK = 0
$returnStateWarning = 1
$returnStateCritical = 2
$returnStateUnknown = 3
## Get lasat installed update and select date as object
$Session = New-Object -ComObject Microsoft.Update.Session
$Searcher = $Session.CreateUpdateSearcher()
$HistoryCount = $Searcher.GetTotalHistoryCount()
## http://msdn.microsoft.com/en-us/library/windows/desktop/aa386532%28v=vs.85%29.aspx
$DatelastInstall = $Searcher.QueryHistory(0,$HistoryCount) | ForEach-Object {$_} | Select-Object -First 1 | Select-Object -Property Date
## Get actual date
$Datenow = Get-Date
## Get number of days since list install
$DayslastInstall = ($Datenow - $DatelastInstall.date).Days
$DayslastInstalldate = ($Datenow.Date - $DatelastInstall.Date).Days
## Debug
# $DatelastInstall.ToString()
# $DatelastInstall
# $DatelastInstall.date
# $Datenow
# $Datenow.date
# $DayslastInstall
# $DayslastInstalldate
if (!$wldays) {
echo "Usage: check_win_last_update.ps1 "
exit 3
}
# if ($DayslastInstall) {
if ($DayslastInstalldate -eq 0) {
Write-Host "OK - Patches applied $DayslastInstalldate days ago at $($DatelastInstall.Date)"
exit $returnStateOK
} elseif ($DayslastInstall -lt $wldays) {
Write-Host "OK - Patches applied $DayslastInstalldate days ago at $($DatelastInstall.Date)"
exit $returnStateOK
} elseif ($DayslastInstall -lt $cldays) {
Write-Host "WARNING - Patches applied $DayslastInstalldate days ago $($DatelastInstall.Date)"
exit $returnStateWarning
} elseif ($DayslastInstall -gt $cldays) {
Write-Host "CRITICAL - Patches applied $DayslastInstalldate days ago $($DatelastInstall.Date)"
exit $returnStateCritical
} else {
Write-Host "UNKNOWN"
exit $returnStateUnknown
}
# } else {
else {
Write-Host "UNKNOWN - unable to get date of last Widows Update Installation"
exit $returnStateUnknown
}
#
# NAME: check_win_last_update.ps1
#
# AUTHOR: Peter Luetke-Bexten
# COAUTHOR: Christoph Hamschmidt
# EMAIL: peter.luetke-bexten (at) benteler.com
#
# COMMENT:
# Script to calculate the Days since last Windows Update
# which is understandable for every Manager :-}
# should be called via NRPE/NSClient++
# inspired by bratislav.stojkovic@gmail.com check_win_lastupdate.vbs
# but with using Microsoft.Update.Session instead of Registry.
# Registry query not working in W2016 or W10.
#
# NSCLIENT.ini:
# check_win_lastupdate = cmd /c echo scriptspowershellcheck_win_last_update.ps1 $ARG1$ $ARG2$ ; exit($lastexitcode) | "C:WindowsSystem32WindowsPowerShellv1.0powershell.exe" -noninteractive -noprofile -ExecutionPolicy unrestricted -command -
#
# NRPE Check:
# check_nrpe -H targethost -p 5666 -t 120 -u -c check_win_lastupdate -a 35 70
#
# CHANGELOG:
# 1.0 20180126 - initial version
#
################################################################################
# Script calculates curent date and date of last Windows Update Installation
## Arguments
$wldays = $args[0]
$cldays = $args[1]
## ReturnStates
$returnStateOK = 0
$returnStateWarning = 1
$returnStateCritical = 2
$returnStateUnknown = 3
## Get lasat installed update and select date as object
$Session = New-Object -ComObject Microsoft.Update.Session
$Searcher = $Session.CreateUpdateSearcher()
$HistoryCount = $Searcher.GetTotalHistoryCount()
## http://msdn.microsoft.com/en-us/library/windows/desktop/aa386532%28v=vs.85%29.aspx
$DatelastInstall = $Searcher.QueryHistory(0,$HistoryCount) | ForEach-Object {$_} | Select-Object -First 1 | Select-Object -Property Date
## Get actual date
$Datenow = Get-Date
## Get number of days since list install
$DayslastInstall = ($Datenow - $DatelastInstall.date).Days
$DayslastInstalldate = ($Datenow.Date - $DatelastInstall.Date).Days
## Debug
# $DatelastInstall.ToString()
# $DatelastInstall
# $DatelastInstall.date
# $Datenow
# $Datenow.date
# $DayslastInstall
# $DayslastInstalldate
if (!$wldays) {
echo "Usage: check_win_last_update.ps1
exit 3
}
# if ($DayslastInstall) {
if ($DayslastInstalldate -eq 0) {
Write-Host "OK - Patches applied $DayslastInstalldate days ago at $($DatelastInstall.Date)"
exit $returnStateOK
} elseif ($DayslastInstall -lt $wldays) {
Write-Host "OK - Patches applied $DayslastInstalldate days ago at $($DatelastInstall.Date)"
exit $returnStateOK
} elseif ($DayslastInstall -lt $cldays) {
Write-Host "WARNING - Patches applied $DayslastInstalldate days ago $($DatelastInstall.Date)"
exit $returnStateWarning
} elseif ($DayslastInstall -gt $cldays) {
Write-Host "CRITICAL - Patches applied $DayslastInstalldate days ago $($DatelastInstall.Date)"
exit $returnStateCritical
} else {
Write-Host "UNKNOWN"
exit $returnStateUnknown
}
# } else {
else {
Write-Host "UNKNOWN - unable to get date of last Widows Update Installation"
exit $returnStateUnknown
}
Reviews (0)
Be the first to review this listing!