Make Windows Task Scheduler alert me on fail

When a scheduled task fails to start, an event is written to the TaskScheduler event log:

Note: The Task Scheduler log is located at (under Administrative Tools)

 Computer Management
    System Tools
       Event Viewer
          Application and Services Logs
             Microsoft
                Windows
                   Task Scheduler
                      Operational

enter image description here

Windows lets you trigger scheduled tasks to start when a variety of events happen, e.g.:

  • time of day
  • system startup
  • user login
  • event recorded in event log

Armed with this knowledge, you can create a scheduled task that that runs when your scheduled task fails:

enter image description here

This scheduled task's action can then be set to something that sends you an alert - in your choice of methods (e.g. triggers a shutdown). You might just want it to send an e-mail:

enter image description here

This is how Windows handles everything. You can see many diagnostic tasks that trigger on an event appearing in the log. e.g. when an IP address conflict is detected, an event is written to the log:

  • Log: System
  • Source: Tcpip
  • Event ID: 4198

A scheduled task triggers on this event, and runs a program to tell you about it and to fix it. Keep in mind that the event id is not specific to just one task. Any task that generates the event 203 - Action failed to start, will trigger this task.


Here is my script to alert me when my backup job has a greater value than 0.

$ScheduledTaskName = "Hans\Backup"
$Result = (schtasks /query /FO LIST /V /TN $ScheduledTaskName  | findstr "Result")
$Result = $Result.substring(12)
$Code = $Result.trim()

If ($Code -gt 0) {
    $User = "[email protected]"
    $Pass = ConvertTo-SecureString -String "myPassword" -AsPlainText -Force
    $Cred = New-Object System.Management.Automation.PSCredential $User, $Pass
################################################################################

$From = "Alert Scheduled Task <[email protected]>"
$To = "Me Gmail <[email protected]>"
$Subject = "Scheduled task 'Backup' failed"
$Body = "Error code: $Code"
$SMTPServer = "smtp.gmail.com"
$SMTPPort = "587"

Send-MailMessage -From $From -to $To -Subject $Subject `
-Body $Body -SmtpServer $SMTPServer -port $SMTPPort -UseSsl `
-Credential $Cred
}

Take a look at PushMon. You can create a PushMon URL that will be called at the end of your script. If your script doesn't run because the server was down or the script was moved, you will get notified. You can get notified by email, SMS, phone call, IM and Twitter. This will work for any operating system. This is like Pingdom but for scripts and background tasks.