I was recently asked to provide a monitoring service for the last success success time of Windows Update and to provide status information within Nagios XI. Basically, the last success time was compared to be run a time span period of a number of days in the past to provide a service state.
In the case of the time span this was dependent on the host being monitored and therefore this period would be specified as a mandatory parameter when invoking the powershell script which was to be used as the check plugin within Nagios.
Param ([parameter(Mandatory = $true)][string] $Days)
We can read the last success time reported from Windows Update by querying the registry to retrieve the date and then format the date string to be in the format ‘dd/MM/yyyy’.
$LastRun = (Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\Results\Install").LastSuccessTime $LastRun = Get-Date $LastRun -Format 'dd/MM/yyyy'
We will now to generate a date in the past based on the value specified by the mandatory Days parameter using the subtraction method from the current date.
In order to generate the service status, we will use conditional logic to compare the current date to that specified in the timespan and to provide a return code and status information message. If the date values cannot be compared this will return an unknown service status.
If ([datetime]::parse($LastRun) -ge [datetime]::parse($TimeSpan)) { $returncode = 0 "Windows Update last run successfully on " + $LastRun } ElseIf ([datetime]::parse($LastRun) -lt [datetime]::parse($TimeSpan)) { $returncode = 1 "Windows Update last run successfully on " + $LastRun } Else { $returncode = 3 }
Finally, we will exit the powershell session by providing the return code.
exit $returncode