Monitor Windows Task Scheduler jobs

Task Scheduler records a last run result for every task, and nobody opens it until something's gone. A Tissue Mast heartbeat expects a beat after every run and pages you when one is missing or reports a failure.

Run the job from a PowerShell script that ends with an Invoke-WebRequest to the heartbeat, carrying the job's exit code. Register the script with schtasks. It works in Windows PowerShell 5.1 and in PowerShell 7.

The PowerShell script

# C:\scripts\backup.ps1
# Runs the backup and reports the run to a Tissue Mast heartbeat.
[Net.ServicePointManager]::SecurityProtocol =
  [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12

$url = (Get-Content -Path 'C:\scripts\heartbeat-url.txt' -Raw).Trim()
$log = 'C:\scripts\backup.log'

try {
  Invoke-WebRequest -Uri "$url/start" -Method Post -UseBasicParsing -TimeoutSec 10 | Out-Null
} catch { }

& 'C:\Program Files\MyBackup\backup.exe' *> $log
$code = $LASTEXITCODE
if ($null -eq $code) { $path = 'fail'; $code = 1 } else { $path = "$code" }

$body = Get-Content -Path $log -Tail 100 | Out-String
try {
  Invoke-WebRequest -Uri "$url/$path" -Method Post -Body $body `
    -UseBasicParsing -TimeoutSec 10 | Out-Null
} catch {
  Write-Warning "Heartbeat not sent: $_"
}

exit $code
heartbeat-url.txt
Holds the heartbeat URL, https://mast.tissue.dev/mk_8e2a…, on one line. It's a write capability, so give the file the same permissions you'd give a password.
$LASTEXITCODE
The exit code of the last native program the script ran. /0 is a beat. Anything from 1 to 255 marks the heartbeat down and pages, with the code on the card. If the program never ran, there's no code, and the script sends /fail instead.
-UseBasicParsing
Windows PowerShell 5.1 needs it on a machine without Internet Explorer, such as Server Core. PowerShell 6 and later ignore it.
-TimeoutSec 10
Without it the request can wait forever. PowerShell 7.4 renamed the parameter -ConnectionTimeoutSeconds and kept -TimeoutSec as an alias, so the same line works in both.
The first line
Windows PowerShell 5.1 runs on .NET Framework, which takes its TLS versions from Windows only from .NET Framework 4.7 on. Adding TLS 1.2 keeps the script working on older servers too.
exit $code
Hands the backup's exit code back to Task Scheduler, so the task's last run result still tells the truth.

The body is the last 100 lines of the log. Mast keeps up to 16 KiB of body with the beat and shows the first 4096 characters in the history, which is where you'll read it after a page.

Registering it with schtasks

From an elevated prompt:

schtasks /create /tn "Nightly backup" /sc daily /st 03:00 /ru SYSTEM /f /tr "powershell.exe -NoProfile -ExecutionPolicy Bypass -File C:\scripts\backup.ps1"
/sc daily /st 03:00
Every day at 03:00. The time is 24-hour and read from the machine's own clock.
/ru SYSTEM
Runs the task as the local SYSTEM account. Give /ru an ordinary account instead if the backup needs one, as long as that account can read heartbeat-url.txt.
-File
Has to be the last parameter. With -File, the script's exit code becomes the exit code of powershell.exe, which is what Task Scheduler records.
/f
Replaces a task of the same name without asking, so you can run the line again after a change. The /tr value can be at most 262 characters.

Run the task once by hand with schtasks /run /tn "Nightly backup" and the first beat shows on the heartbeat.

The heartbeat to go with it

Give the heartbeat the task's time and the machine's time zone, as an IANA name:

curl -X PATCH https://api.tissue.systems/v1/mast/alert-sources/nightly-backup \
  -H "Authorization: Bearer $TISSUE_TOKEN" -H "Content-Type: application/json" \
  -d '{"schedule": "0 3 * * *", "schedule_tz": "America/Chicago",
       "grace_secs": 1800, "max_duration_secs": 5400}'

Or in the web app, under Heartbeats. The grace is how long after 03:00 the beat may arrive before anyone is woken. The longest run catches a backup that sent /start and is still going 90 minutes later. A machine that was off, asleep or rebuilt without its task sends nothing at all, and that pages when the grace runs out.

Free is ten heartbeats with an email address and no credit card. Team is $29 a month for 250 heartbeats, with rotations and escalation for everyone on call.

Create a free account Ten heartbeats, ten probes, ten alert sources.

Read more