Monitoring Windows Task Scheduler jobs with a heartbeat
Windows Task Scheduler records "Last Run Result" codes, but nobody looks at them until something is missing. A heartbeat turns a silent failure into an alert.
PowerShell wrapper
# C:\jobs\run-with-heartbeat.ps1
param([string]$Url, [string]$Command, [string[]]$Arguments)
try { Invoke-RestMethod -Uri "$Url/start" -TimeoutSec 10 | Out-Null } catch {}
$output = & $Command @Arguments 2>&1 | Out-String
$code = $LASTEXITCODE
if ($null -eq $code) { $code = 0 }
$tail = if ($output.Length -gt 9000) { $output.Substring($output.Length - 9000) } else { $output }
try { Invoke-RestMethod -Method Post -Uri "$Url/$code" -Body $tail -TimeoutSec 10 | Out-Null } catch {}
exit $code
Scheduled task action
Program: powershell.exe
Arguments: -NoProfile -ExecutionPolicy Bypass -File C:\jobs\run-with-heartbeat.ps1
-Url https://cronpup.com/p/UUID -Command C:\jobs\export.exe -Arguments "--full"
Exit code 0 reports success. Anything else alerts immediately, with the last 9 KB of output attached. If the task
stops being triggered at all (it was disabled, the password expired, the machine was retired), the missing ping alerts you
when the grace period runs out.