Fun with scheduled tasks in Windows

Fun with scheduled tasksAs many people know, Microsoft Windows has a built-in task scheduler (like *nix cron) to run programs at particular times. Recently at work, I’ve had to start using the Microsoft Windows Server 2008/Vista Task Scheduler (2.0) which is fairly different from the Windows Server 2003/Windows XP (1.0) Task Scheduler (2.0). Read on…

For those who haven’t used the Task Scheduler 2.0, it’s actually pretty handy, especially compared to the original Task Scheduler. Here are some benefits:

The Good

Triggers
Instead of just being able to run the task at a specific time, you now have the ability to trigger a task to start with several events called triggers:

  • On a schedule
  • At log on
  • At startup
  • On idle
  • On an event
  • At task creation/modification
  • On connection to user session
  • On disconnect from user session
  • On workstation lock
  • On workstation unlock

The first option, “On a schedule”, gives you the classic task scheduler… scheduling. And on top of that, you can define multiple triggers on a given task. So if you need a task to run at multiple times a day, you no longer need to define multiple jobs in the task scheduler, just define one job with multiple triggers.

Actions
Similar to the enhancements regarding triggers, you have the ability to define actions for a task now, instead of just a program to run. Actions include:

  • Start a program
  • Send an e-mail
  • Display a message

The first option, “Start a program”, gives you the classic task scheduler option of specifying the program to start when the scheduler fires for that task. And much like triggers, you can define multiple actions for a given task.

Import/Export
You can now export your job definitions from the Task Scheduler as plain ol’ XML. This is useful for doing things like putting your job definitions in version control (I still need to put together a tool to do that for me automatically :-)). And you can then import those XML definitions back into the Task Scheduler.

So if you need to create multiple jobs that are very similar to each (just have some different command args, names, etc.), I’ve found it easy to create one task as a sort of template, export it to XML, copy the XML for each other job I need, modify each XML for whatever little tweak it needs, rename the file (the Task Scheduler uses the XML file name as the imported Task name), then import the file back in. Personally I’m faster at doing that than clicking through all the Create Task screens.

Folders
You can define a folder hierarchy to organize tasks, much like on your hard drive you create a directory hierarchy to organize files. This is really useful on machines that have dozens of various tasks defined.

The Bad

Renaming Objects… or not
Maybe this is just a permission problem on my specific machine, but I have yet to find a way to rename folders in the Task Scheduler, or Tasks themselves! Right now my workaround is to export the Task to XML, rename the file, then import it back in. Similarly, I just remove folders and recreate them with the new name. Pretty tedious stuff, especially when it needs to be done for lots of tasks.

The Ugly

No Migration Utilities
Maybe I didn’t look hard enough, but I spent some time Googling around for a way to convert the Windows Server 2003/XP .job scheduled task files into the Windows Server 2008/Vista XML scheduled task files. After I stumbled across this, I gave up: http://social.technet.microsoft.com/Forums/en-US/ itprovistamigration/thread/76b6276b-49f2-41e2-b4ea- f537e0eb88a8. From a Microsoft moderator in this thread:

We cannot import .job files to Task Scheduler into Windows Vista . In addition, we have no way to convert .job files to .xml files. Therefore, we cannot run schedule tasks created on previous systems on Windows Vista/Server 2008.

Why does this not surprise me. (don’t answer that)

Well, at work I had the task to move about 100+ scheduled tasks from one machine to another. Unfortunately, the source machine was Windows Server 2003, and the target machine was Windows Server 2008, and I was not about to recreate 100+ scheduled tasks by hand. Forutnately, I was able to script the process. While I can’t give away the code verbatim, I’ll give you a rough idea as to what I did:

First, I used the Windows Server 2003/XP schtasks (with the /query /fo csv /v args) to get a nice little CSV file of all the scheduled tasks on the source machine.

Then, I exported a dummy scheduled task from a Windows Server 2008 box and created a sort of template job definition file. I put in place holders for things like the user to run the job as, the command line, the schedule, etc.

Then, I whipped up a little Perl script, using Text::CSV_XS for CSV parsing, to read in the CSV, massage the data a little, and used my template XML job definition to create the 100+ job XML files. The whole process runs in less than a minute, not too shabby.

Now, today I just need to find a way to automatically import these jobs. So if you too are looking for a way to convert Windows Server 2003/XP .job files into Windows Server 2008/Vista XML task definition files, hopefully this helps. I haven’t really looked into a C# API for the Task Scheduler 2.0, but this looks promising. Happy task scheduling!

2 thoughts on “Fun with scheduled tasks in Windows

  1. Chris

    I don’t suppose you still have the “PERL script” you used to convert the CSV file to a valid XML file do you? I’m in the same boat, I have a few hundred jobs to convert to our new Win7 machine.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *