Monitoring Scheduled Tasks with PRTG

Because PRTG doesn’t have a built-in sensor for Windows Scheduled Tasks, I created this PowerShell script. It’s easy to use, flexible, and gives clear results.

You can now:

  • Monitor any scheduled task on a remote server
  • Get alerts when tasks fail or stop running
  • View task results directly inside PRTG

Feel free to copy, adjust, and improve this script for your environment.

Download the Script here on GitHub

What the Script Does

This script connects to a remote Windows server and checks the last result of a scheduled task.

It takes two parameters:
-server: The name or IP of the remote host. In PRTG this is usually %host.
-taskname: The name of the scheduled task (as seen in Task Scheduler).

Uses Invoke-Command to run a PowerShell command on the remote server.

Gets the last result code of the task using Get-ScheduledTaskInfo.

Converts that result into a simple number for PRTG:

  • 0 = Completed successfully
  • 41301 = Currently running
  • Any other value = Error

Outputs the result as XML, which PRTG understands as a custom sensor.

Save the Script scheduledTask_sensor.ps1 under
C:\Program Files (x86)\PRTG Network Monitor\Custom Sensors\EXE\

 <#
 
 Author: S.Meier
 V1.0
        
        Parameter:
        -server %host
        -taskname = Task Name

#>

param (
    [string]$Server,
    [string]$taskname
)


$taskinfo = Invoke-Command -ComputerName $Server -ScriptBlock { {0:x} -f (Get-ScheduledTaskInfo -TaskName $using:taskname).LastTaskResult }

switch($taskinfo){
    "0" {$lastresult = "0"}
    "41301" {$lastresult = "1"}
    default {$lastresult = "2"}
}

$XMLOutput = "<prtg>
  <result>
     <channel>Last Result</channel>
    <value>$lastresult</value> 
    <CustomUnit>count</CustomUnit>
  </result>
</prtg>
"

$XMLOutput

Lookup File for PRTG

The lookup file tells PRTG how to interpret the result values from the script.

The script returns numbers like 0, 1, or 2. These numbers don’t mean anything to users unless we tell PRTG what they stand for.

That’s where the lookup file helps. It maps each number to a readable status like this:

ValuePRTG StateShown Text
0OKCompleted Successfully
1OKCurrently Running
2ErrorError

You need to add the lookup file scheduled.task.lookup.ovl to PRTG:

<?xml version="1.0" encoding="UTF-8"?>
<ValueLookup id="scheduled.task.lookup" desiredValue="0" undefinedState="Warning">
  <Lookups>
    <SingleInt state="OK" value="0">Completed Successfully</SingleInt>
    <SingleInt state="OK" value="1">Currently Running</SingleInt>
    <SingleInt state="Error" value="2">Error</SingleInt>
  </Lookups>
</ValueLookup>

Save it under: C:\Program Files (x86)\PRTG Network Monitor\lookups\custom\
Then reload the lookups in PRTG under Setup > System Administration > Administrative Tools > Load Lookups.

How to Use in PRTG

1. Create a new EXE/Script Advanced Sensor in PRTG and add the Powershell script.
2. Set Parameters: -server %host -taskname “YourTaskName
3. Open the Channel settings and chose “Enable alerting based on lookups” then add the Lookup file.

Now the Chanel should look like this:

Leave a Reply

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

Proudly powered by WordPress | Theme: Wanderz Blog by Crimson Themes.