← Docs

Monitor a Windows Task Scheduler job with PowerShell

In Task Scheduler add a second action (or end your PowerShell script) that calls the ping URL:

# success
Invoke-RestMethod -Uri "https://heartbeat-monitor.morristech.ca/ping/YOUR-CHECK-ID" -TimeoutSec 10 | Out-Null

# full pattern
try {
  Invoke-RestMethod "https://heartbeat-monitor.morristech.ca/ping/YOUR-CHECK-ID/start" -TimeoutSec 10 | Out-Null
  & C:\Scripts\Nightly.ps1
  if ($LASTEXITCODE -ne 0) { throw "exit $LASTEXITCODE" }
  Invoke-RestMethod "https://heartbeat-monitor.morristech.ca/ping/YOUR-CHECK-ID" -TimeoutSec 10 | Out-Null
} catch {
  Invoke-RestMethod "https://heartbeat-monitor.morristech.ca/ping/YOUR-CHECK-ID/fail" -Method Post -Body $_.Exception.Message -TimeoutSec 10 | Out-Null
}

Older systems can use curl.exe -fsS https://heartbeat-monitor.morristech.ca/ping/YOUR-CHECK-ID (curl ships with Windows 10 and later).