Monitor a Windows scheduled task
Write the task's trigger as cron, then wrap the job in a PowerShell script that reports its exit code.
1. Create the monitor from the trigger
Task Scheduler does not use cron, so write the trigger as a Unix cron expression, or use an interval monitor for a repeating trigger. A daily trigger at 02:00 is 0 2 * * *. Pick the machine's time zone, because triggers run in local time.
2. Wrap the job in a script
The script reports the result and keeps the job's own exit code. It sends /fail instead of the exit code form, because Windows exit codes can be larger than the 0 to 255 that the ping URL accepts.
# C:\jobs\backup.ps1
$ping = "https://ping.cronwize.com/YOUR-KEY"
# A failed ping must never fail the job.
function Send-Ping($signal) {
try {
Invoke-RestMethod -Uri "$ping$signal" -TimeoutSec 10 | Out-Null
} catch {
Write-Warning "Cronwize ping failed: $_"
}
}
Send-Ping "/start"
& "C:\jobs\backup.exe"
$code = $LASTEXITCODE
if ($code -eq 0) {
Send-Ping ""
} else {
Send-Ping "/fail"
}
exit $code3. Register the task
$action = New-ScheduledTaskAction -Execute "powershell.exe" -Argument "-NoProfile -ExecutionPolicy Bypass -File C:\jobs\backup.ps1"
$trigger = New-ScheduledTaskTrigger -Daily -At 2am
Register-ScheduledTask -TaskName "nightly-backup" -Action $action -Trigger $trigger -User "SYSTEM"SYSTEM runs the task whether or not anyone is signed in. Keep the script readable only by the accounts that need it, because the key in the URL is the only credential a ping needs.
4. Check the first ping
Run the task once. The monitor turns from new to up.
Start-ScheduledTask -TaskName "nightly-backup"