Home Directory Plugins Operating Systems Windows check_win_last_update.ps1

Search Exchange

Search All Sites

Nagios Live Webinars

Let our experts show you how Nagios can help your organization.

Contact Us

Phone: 1-888-NAGIOS-1
Email: sales@nagios.com

Login

Remember Me

check_win_last_update.ps1

Rating
0 votes
Favoured:
0
Current Version
1.o
Last Release Date
2018-01-26
Compatible With
  • Nagios 1.x
  • Nagios 2.x
  • Nagios 3.x
  • Nagios 4.x
Owner
License
GPL
Hits
655
Network Monitoring Software - Download Nagios XI
Log Management Software - Nagios Log Server - Download
Netflow Analysis Software - Nagios Network Analyzer - Download
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.
##################################################################################
#
# 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
}