Make Windows Task Scheduler alert me on fail

69

32

I have an automated script that pulls backups from my website to my local computer. This script could fail; once my server was down, another time I accidentally moved my script.

How do I make Windows Task Scheduler tell me with the script fails (or doesn't run/not found)?

I don't care if a prompt comes up, an email or something that appears on my desktop. I want to be notified if something goes wrong. On my server, crontab emails me about errors - which is great. I want something like that on my windows 7 local computer.

user3109

Posted 2011-02-22T21:15:46.033

Reputation:

found the task scheduler event IDs at least https://mnaoumov.wordpress.com/2014/05/15/task-scheduler-event-ids/

– None – 2016-05-25T20:50:35.020

Answers

89

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.

Ian Boyd

Posted 2011-02-22T21:15:46.033

Reputation: 18 244

2

Here are some links to the list of event ids so you can monitor other types of failures: http://technet.microsoft.com/en-us/library/dd315533%28v=ws.10%29.aspx and http://technet.microsoft.com/en-us/library/dd363625%28v=ws.10%29.aspx

– Chris Smith – 2015-01-08T23:25:00.603

@jwg you can choose "Custom" instead of "Basic" Settings and select multiple event ids to include "Task Failed" – Marc – 2016-09-29T06:52:45.977

2This answer is now out-of-date. I checked Windows 10 and Windows Server 2012 R2, the send email action has been deprecated. – TheCrazyProgrammer – 2017-05-04T05:58:15.340

Any ideas how to approach this now that Microsoft so brilliantly nuked this incredibly useful feature? – John Hargrove – 2018-03-13T00:54:36.093

1@JohnHargrove You're now supposed to execute a powershell script. The steps involved are so complicated that it's not worth even learning. – Ian Boyd – 2018-03-13T12:17:08.887

@jwg If you create a second action in the task then that would fail to start and you should get a 203. Would that work to alert if a task fails? I'm assuming that Scheduler fires the tasks sequentially and not concurrently. – Caltor – 2019-10-21T12:42:08.063

+1. How do i get the event id? i TasKScheduler doesnt show up for me. I tried making a custom event using the advance button which had a nice UI to help. But at the end of it, it said request not available. Note i am using windows home premium – None – 2011-02-22T22:59:06.490

These screenshots are from Windows 7. Windows XP doesn't support it. Windows Vista: i don't know. What version of Windows do you have? – Ian Boyd – 2011-02-22T23:42:25.513

Ian Boyd: Windows7 "home premium". Here is what i see http://i.imgur.com/Qd8QN.png

– None – 2011-02-23T00:10:41.427

That screenshot is the TaskManager, from inside the Task Scheduler. Do you see Task Scheduler in the Event Log? – Ian Boyd – 2011-02-23T03:18:24.943

2You get the event id from the event log (203 and 103). You can see the event id's in the screenshot (203 and 103). Or i can tell you the event id's: 203 and 103. – Ian Boyd – 2011-02-23T03:20:42.117

5What do you do if the second task fails!?!? – William Jackson – 2012-06-01T14:06:00.820

4@WilliamJackson Create an event that shuts down the machine. Someone will definitely be alerted to a problem then. – Ian Boyd – 2012-06-01T17:51:04.127

11Actually if the 2nd task fails it will launch the 2nd task which will fail which will launch the 2nd task which will fail which will launch the 2nd task which will... you get the idea. I just found this out the hard way =/ – Rob Penridge – 2012-10-19T20:58:37.820

6This doesn't alert you if a task fails, only if it fails to start. Not quite the same thing. – jwg – 2013-08-20T15:44:25.340

14

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 = "mymail@gmail.com"
    $Pass = ConvertTo-SecureString -String "myPassword" -AsPlainText -Force
    $Cred = New-Object System.Management.Automation.PSCredential $User, $Pass
################################################################################

$From = "Alert Scheduled Task <mymail@gmail.com>"
$To = "Me Gmail <mymail@gmail.com>"
$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
}

Hans Falk

Posted 2011-02-22T21:15:46.033

Reputation: 141

This is a good option as the current selected answer uses a deprecated Windows feature. – John Hargrove – 2018-03-13T02:51:36.500

1Please keep in mind, that this is specifically crafted solution for users with Windows in english language. The argument for ´findstr´ must be changed accordingly to user's language set in Windows. – Eda190 – 2018-10-05T13:59:30.590

7

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.

Bienvenido David

Posted 2011-02-22T21:15:46.033

Reputation: 171

1Trying to visit their site; ironically, it seems to be down.. – Isaac Kleinman – 2016-07-13T18:22:31.530

1If you add it to the end of your script, and the script is missing (e.g. moved), how is it supposed to get called‽ – Bob – 2012-06-01T14:24:53.527

3Since the script moved and URL did not get called, then you will get an alert. – Bienvenido David – 2012-06-01T20:27:39.703