In "Scripting Solutions with WSH and COM: Give Your PCs a Wake-Up Call," February 2002, InstantDoc ID 23602, I demonstrated how to use Wake on LAN (WOL) technology to wake up your PCs. Unless you plan to stay awake with your computers all night so that you can wake them up and run the scripts manually, you need a way to schedule tasks automatically. I introduce you to the power of automatically scheduling your scripts on Windows XP systems and provide a few simple scripts that show you how automated tasks work.
The Good Old Cron Days
Since the early days of PC systems management, I've used a dedicated PC in the data center rather than a server to run scheduled tasks automatically across the network. When I began searching for software that I could use to schedule tasks, I wanted something that worked similarly to the UNIX cron daemon. This daemon schedules automated tasks according to entries in stored configuration files called crontabs, then outputs the results to log files called cronlogs. I discovered a PC version of this UNIX utility that runs permanently in DOS, scheduling tasks that I add to the crontab file. To this day, my dedicated scheduling PC is known as "the cron machine," even though I've upgraded it to XP so that it can run all automated tasks across the network.
Before Microsoft developed the Task Scheduler service, you had to use the At command from the command line and the Schedule service to schedule tasks in Windows NT. In fact, many users referred to the service as AT because it ran tasks at a time and date. AT had many useful commands, such as Net Stop Spooler. The sample command that Listing 1 shows tells the Schedule service to run the Net Stop Spooler command next Thursday at 2:15 a.m., give the task job ID 1, and let it interact with that server's desktop. You can schedule scripts with familiar extensions, such as .cmd, .bat, and .vbs, to run. You can also use the At command to list all the jobs scheduled, then use the Delete command to delete a job. The code that Listing 2 shows deletes job 1.
Unfortunately, AT's UI was clunky, so Microsoft developed the WinAT scheduler, which shipped with the Microsoft Windows NT Server 4.0 Resource Kit. WinAT had a Windows-type GUI for managing the scheduling service. Despite the improved GUI, WinAT was still clunky and not very usable. But everything changed when Microsoft released Windows 2000. The new OS brought with it a scripter's delight in the form of the brand-new Task Scheduler service. I quickly upgraded my cron machine, and I've never looked back. Since then, I've upgraded the machine again to XP. Let's take a look at the XP Task Scheduler service's administrative software.
Scheduling Tasks in Task Scheduler
Although I'm using XP for my examples, you can just as easily apply what I'm describing here to the Win2K Task Scheduler. (Note that AT still exists in XP and Win2K if you want to use it; the OS adds AT tasks to the Scheduled Tasks folder.)
Task Scheduler is a service, and you can view it in the Microsoft Management Console (MMC) Services snap-in. (Note that Services is a standalone snap-in that you must add.) If you stop the Task Scheduler service, tasks won't run at their scheduled times. (By default, the Task Scheduler service is running.) You access the Task Scheduler service's GUI for scheduling tasksi.e., the Scheduled Tasks GUIthrough the Scheduled Tasks system folder. To open the Scheduled Tasks folder (by default, C:\windows\tasks) in XP, choose Start, All Programs, Accessories, System Tools, Scheduled Tasks. Doing so opens the Scheduled Tasks GUI, which Figure 1 shows. If you haven't scheduled tasks, only the Add Scheduled Task icon is visible. However, in Figure 1, you can see the AT task (i.e., At1) that the System account created earlier and two other tasks: Create User Accounts and SendMagicPacket. In the Scheduled Tasks folder, these two tasks appear as files with a .job extension.
From the Scheduled Tasks GUI, you can schedule any script, program, or even document to run at a time that's most convenient for you. For example, you can schedule a task to run daily, weekly, monthly, or at certain times (e.g., at system startup); change the schedule for a task; stop a scheduled task; or customize how a task will run at a scheduled time.
Adding a Task
To add a task, you double-click the Add Scheduled Task icon in the Scheduled Tasks GUI to start the Scheduled Task Wizard. I want to schedule a slightly modified form of SendMagicPacket.vbs from last month's article to wake up a set of PCs by setting the objWOL properties for each machine, then sending the magic packet to each machine in turn. Listing 3 shows the modified code for SendMagicPacket.vbs.
On the first page of the wizard, I browse to SendMagicPacket.vbs and select it as the application (or, in this case, script) that I want to run. Next, I schedule the script to run daily, as Figure 2 shows. On the next page, I specify the date on which the task should begin running (i.e., March 1, 2002) and the time at which it should regularly run (i.e., 2:00 a.m.). Now, I add the username and password under which the task will run. Finally, a summary of the task details appears. Figure 1 shows the finished task in the Task Scheduler GUI.