Monitor scheduled Tasks using PowerShell and check_mk

2. February 2017

PowerShell Version: >1 
Modules: none

As I am focused on a new check_mk implementation there might be a lot of PowerShell/check_mk around here for a bit 😉 This time I wrote something to monitor the result of a scheduled task the oldschool way. With PowerShell >4 you can use the Get-ScheduledTask function as shown here but as I want this to run even on PowerShell v1 I used the old schtasks /query command line function:

#enter task name with path in scheduler
$name = "TEST"
#gather information
$scheduled_task = schtasks /query /TN $name /v /fo LIST
# EN - "Last Result" ; DE - "Letztes Ergebnis"
$scheduled_task_result = $scheduled_task | Select-String -Pattern "Last Result"
#fix name for check_mk
if ($name -like "*\*")
    $name = $name -replace '.*\\',''
#task errorstate 1
if ($scheduled_task_result -like "*1*")
    echo `<`<`<local`>`>`>
    echo "2 $name $name=1 Scheduled Task not successful!"
#task successful
elseif ($scheduled_task_result -like "*0*")
    echo `<`<`<local`>`>`>
    echo "0 $name $name=0 Scheduled Task successful!"
#task errorstate $?
    echo `<`<`<local`>`>`>
    echo "1 $name $name=2 Status unclear!"
#check_mk #PowerShell

Leave a Reply

Your email address will not be published. Required fields are marked *