Monitoring Task Scheduler Tool (MoTaSh)

This is now an Open Source project, get the latest version and the source code from GitHub

Since Vista, Microsoft Windows comes with a much improved task scheduler, which is pretty cool.
However when running many tasks, some of them may fail once in a while. Unless you have reporting built into those tasks it is not easy to find out when they failed.

You could use the Task Scheduler GUI and review the 'Last Run Result' column, there should be a (0x0) for zero if the tasks ran without problems. Any other number usually indicates a problem.

But what if the last ran was successful, but the previous one wasn't. You would need to get into the history of the task and browser through all the entries there.

The Windows event log has an informational entry for each run, whether it was successful or not, so this is also not very helpful unless you want to search all event messages with a Regex like 'with return code -?[1-9]+\d*\.'

Luckily Microsoft also gave us a much improved API for the new Tasks Scheduler, it is COM based but we can use it from Powershell or DOT.NET to get to the RegisteredTask object which as a property of 'LastTaskResult'

So I wrote a small tool that checks the tasks for a LastTaskResult <> 0 and sends an email to the admin.
I could have written this as a Powershell script or Console application to run once an hour, but then I would have to schedule it with the same service I want it to monitor. So instead I wrote a Windows Service which runs in the background and checks once an hour. This makes it slightly more complex to install but it is the only other built-in way to run an process regularly.

Requirements
- Windows NT 6 or higher (Vista, 7, 2008, 2008 R2)
- DOT.NET Framework 3.5 (included in 7 and R2)
- Administrative permissions to install the service.
Installation
- Download the zip file from GitHub
- Extract the files into a directory of your choice
- Open a command prompt (cmd.exe) and run the installservice.cmd batch
- Open the Services console (services.msc) and locate the 'Hacon Motash' service.
- Change the 'Startup Type' to Automatic
- Open the 'Hacon.Motash.Service.exe.config' file and adjust it, see below.
- Start the service.
- To uninstall the service, use the second line in the installservice.cmd batch file.
- To test this tool, add a new task pointing to an executable that does not exists.
Configuration
The configuration is in the Hacon.Motash.Service.exe.config file which you can edit in Notepad.
We need to change the email settings. Under MailSettings you need to configure your SMTP server in the host attribute. If your server requires you to log on to send mail, provide a username and password as well.
tnlSenderEmail is the address used as the sender, AlertRecipient is the address to send the email to.
The last setting to set is 'RootFolderPattern', it should be the name of the folder you want to monitor. If you created a top level folder 'FooCompany' in Task Schedules, use 'FooCompany' here. If you want to monitor more than one folder use a pipe character to separate them e.g. 'FooCompany|BarInc'

When you make changes to the configuration, you have to restart the service.
Changes in Vs. 1.3
- When a task that runs once a day failed, Motash would report about it every hour until it may not fail the next run. Now it looks only at tasks that ran within the last hour and failed.
- When a task is running, it has a result code not equal to zero. If Motash checks while a tasks is still running (some tasks run for hours) it would report a running task as a failed task. Now it does ignore running tasks as well as disabled tasks.
- Robocopy.exe returns with Exit codes of 1, 2 or 3 even if the copy operation succeeded. So a failure was reported by MoTaSh. To allow exit codes other than zero, put a comma separated list of integers inside curly brackets anywhere into the description field of the task. For a robocopy task this could be:
Copy files, allow exit codes {0,1,2,3}, cool stuff!
Make sure to include zero. If no such list is found, only zero is considered a 'success' exit code.
- The Service now runs under 'Local Service' rather than 'Local System'.
Known Issues
- Tasks to monitor must be in a folder, not in the root
- Only the top three folder levels are monitored
- Some tasks fail and still exit with a return value of zero, there is no way for Motash to detect this. One example is a Powershell script with a syntax error.
- The user has no control over the email content or layout.

Pages in this section