<# Version: 0.1 Date: Aug 19, 2020 Author: ivan.stetka@live.com Summary: This is a Nagios NCPA plugin to check windows ScheduedTask -N, --taskName # name of the scheduled task -P, --taskPath # path of the scheduled task -c, --crit # critical threshold (the maximum number of hours since the task was last run) # example: 25 (you can also use floating numbers like 24.5) On host side copy plugin to folder 'C:\Program Files (x86)\Nagios\NCPA\plugins\' Nagios example: ./check_ncpa.py -H '' -t '' -M 'plugins/check_ScheduledTask.ps1' -a "-N 'Microsoft Compatibility Appraiser' -P '\\Microsoft\\Windows\\Application Experience\\' -c 25" #> param ( [parameter(Mandatory = $true)] [Alias("N")] [string] $taskName, [parameter(Mandatory = $true)] [Alias("P")] [string] $taskPath, [parameter(Mandatory = $true)] [Alias("c")] [float] $crit ) $taskName = invoke-expression $taskName $taskPath = invoke-expression $taskPath $STATES = @{ OK = 0; WARNING = 1; CRITICAL= 2; UNKNOWN = 3 } $pluralM = 's'; $pluralH = 's'; $pluralD = 's'; $taskState = (Get-ScheduledTask -TaskName $taskName -TaskPath $taskPath -ErrorAction SilentlyContinue).state if (!$taskState) { Write-Output "CRITICAL: Sheduled task not found, is the name '$taskName' or the path '$taskPath' of task correct?" Exit($STATES.CRITICAL) } try { $dateTask = ((Get-ScheduledTask -TaskName $taskName -TaskPath $taskPath -ErrorAction SilentlyContinue) | Get-ScheduledTaskInfo).LastRunTime $diffDate = (Get-Date)-$dateTask $totalDiffHours = $diffDate.TotalHours $diffDays = $diffDate.Days $diffHours = $diffDate.Hours $diffMinutes = $diffDate.Minutes if ($diffMinutes -eq 1) {$pluralM = '';} if ($diffHours -eq 1) {$pluralH = '';} if ($diffDays -eq 1) {$pluralD = '';} if ($totalDiffHours -ge 24) { $timeText = "$diffDays day$pluralD $diffHours hour$pluralH and $diffMinutes minute$pluralM"; }else { $timeText = "$diffHours hour$pluralH and $diffMinutes minute$pluralM" } $taskInfo = ((Get-ScheduledTask -TaskName $taskName -TaskPath $taskPath) | Get-ScheduledTaskInfo) | Format-List -property TaskName, TaskPath, LastRunTime, NextRunTime, NumberOfMissedRuns $additTextS = "State of the scheduled task $taskName is $taskState" $additTextT = "Last running of task $taskName was before $timeText" if ($taskState -eq "Disabled") { Write-Output "CRITICAL: $additTextS! $additTextT." Write-Output @taskInfo Exit 0 }elseif ($totalDiffHours -lt $crit) { Write-Output "OK: $additTextT. $additTextS." Write-Output @taskInfo Exit 0 }else { Write-Output "CRITICAL: $additTextT! $additTextS." Write-Output @taskInfo Exit 2 } } catch { Write-Output "WARNING: unexpected error" Exit 1 }