Windows Task Scheduler
Windows scheduled tasks and PowerShell.
Windows Task Scheduler shows the last result as a code like 0x41303, and noticing that a task has not run for a week requires opening the console. Reporting outwards removes that need.
$Ping = 'https://ping.tickwatch.dev/YOUR-KEY'
function Send-Ping {
param([string]$Suffix = '', [string]$Body = '')
$url = if ($Suffix) { "$Ping/$Suffix" } else { $Ping }
try {
Invoke-RestMethod -Uri $url -Method Post -Body $Body -TimeoutSec 10 | Out-Null
} catch {
# Молчим: недоступность мониторинга не должна ломать задачу.
}
}
Send-Ping -Suffix 'start'
try {
$out = & 'C:\Scripts\job.cmd' 2>&1 | Out-String
Send-Ping -Body $out
} catch {
Send-Ping -Suffix 'fail' -Body ($_ | Out-String)
exit 1
}Registering the task in one command:
$action = New-ScheduledTaskAction -Execute 'powershell.exe' `
-Argument '-NoProfile -ExecutionPolicy Bypass -File C:\Scripts\job.ps1'
$trigger = New-ScheduledTaskTrigger -Daily -At 3am
Register-ScheduledTask -TaskName 'Nightly job' -Action $action -Trigger $trigger `
-User 'SYSTEM' -RunLevel Highest