How to use scheduled tasks
Microsoft® Windows® 2000 Scripting Guide
- How to access Scheduled Tasks
- Win32_ScheduledJob Properties
- Scripting Steps
- Enumerating Scheduled Tasks
- Example of Setting a scheduled task
Scheduled tasks can save a great deal of work for administrators, especially if these tasks are well coordinated for minimal impact on users, computers, and the network. However, poorly planned scheduled tasks can cause a number of problems.
For example, if multiple tasks are scheduled to run at the same time, some tasks might interfere with other tasks. Likewise, CPU-intensive or network-intensive tasks scheduled to run at inopportune times might negatively affect users or the network.
To help ensure that tasks are carried out on a regular basis, but without adversely affecting users or the network, it is important for administrators to know which tasks are scheduled to run on their computers and when they are scheduled to run. Enumerating your scheduled tasks can help you minimize the impact of these activities on an individual computer and on the network as a whole.
The Win32_ScheduledJob class can be used to enumerate the following items about the scheduled tasks on a computer:
- Which task is scheduled to run.
- When the task is scheduled to run.
- What happened the last time the task was scheduled to run. (That is, did the task run as expected, or did it fail?)
Scheduled task properties available through the Win32_ScheduledJob class are shown in Table 17.13.
How to access Scheduled Tasks (Click the hyperlink for assistance with that step)
- Open System Tools
- Double-click Scheduled Tasks
Table 17.13 Win32_ScheduledJob Properties
Property | Description |
Caption |
Short description (one-line string) of the task. |
Command |
Name of the command, batch program, or binary file (along with command-line arguments) that the schedule service will use to invoke the job. Example: "defrag /q /f" |
DaysOfMonth |
Days of the
month when the job is scheduled to run. If a job is to run
on multiple days of the month, these values can be joined in
a logical OR. For example, if a job is to run on the 1st and
16th of each month, the value of the DaysOfMonth property
will be 1 OR 32768.
Values include: 1 - 1st 2 - 2nd 4 - 3rd 8 - 4th 16 - 5th 32 - 6th 64 - 7th 128 - 8th 256 - 9th 512 - 10th 1024 - 11th 2048 - 12th 4096 - 13th 8192 - 14th 16384 - 15th 32768 - 16th 65536 - 17th 131072 - 18th 262144 - 19th 524288 - 20th 1048576 - 21st 2097152 - 22nd 4194304 - 23rd 8388608 - 24th 16777216 - 25th 33554432 - 26th 67108864 - 27th 134217728 - 28th 268435456 - 29th 536870912 - 30th 1073741824 - 31st |
DaysOfWeek |
Days of the week
when the job is scheduled to run. If a job is to run on
multiple days of the week, these values can be joined in a
logical OR. For example, if a job is to run on Mondays,
Wednesdays, and Fridays, the value of the DaysOfWeek
property will be 1 OR 4 OR 16.
Values include: 1 - Monday 2 - Tuesday 4 - Wednesday 8 - Thursday 16 - Friday 32 - Saturday 64 - Sunday |
Description |
Description of the object. |
ElapsedTime |
Length of time that the job has been executing. |
InstallDate |
Date the job was created. |
InteractWithDesktop |
Boolean value indicating that the specified job is interactive (meaning a user can give input to a scheduled job while it is executing). |
JobID |
Identifier number of the scheduled task. |
JobStatus |
Status of
execution the last time this job was supposed to run. Values
are: Success Failure |
Notify |
User to be notified upon job completion or failure. |
Owner |
User that submitted the job. |
Priority |
Urgency or importance of execution of a job. |
RunRepeatedly |
Scheduled job should run repeatedly on the days that the job is scheduled. If FALSE, the job is run once. |
StartTime |
UTC time to run
the job, in the format: YYYYMMDDHHMMSS.MMMMMM±UUU Where YYYYMMDD must be replaced by ********. The replacement is necessary because the scheduling service allows jobs to be configured to run only once or to run on a day of the month or week. A job cannot be run on a specific date. For example, ********123000.000000-420 means that the task should run at 12:30 P.M. Pacific time with daylight saving time in effect. In the Universal Time Coordinate (UTC) format: yyyy represents the year. mm represents the month. dd represents the day. HH represents the hour (in 24-hour format). MM represents the minutes. SS represents the seconds. xxxxxx represents the milliseconds. ±UUU represents the number of minutes difference between the current time zone and Greenwich mean time. |
TimeSubmitted |
Time that the job was created. |
UntilTime |
Time after which the job is invalid or should be stopped. |
Scripting Steps
Listing 17.29 contains a script that enumerates the scheduled tasks on a computer. To carry out this task, the script must perform the following steps:
- Create a variable to specify the computer name.
- Use a GetObject call to connect to the WMI namespace root\cimv2 on the computer, and set the impersonation level to "impersonate."
-
Use the ExecQuery method to query the Win32_ScheduledJob class.
This query returns a collection consisting of all the scheduled tasks created for the computer.
- For each scheduled task in the collection, echo the task properties.
Listing 17.29 Enumerating Scheduled Tasks
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
strComputer = "." Set objWMIService = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") Set colScheduledJobs = objWMIService.ExecQuery _ ("SELECT * FROM Win32_ScheduledJob") For Each objJob in colScheduledJobs Wscript.Echo "Caption: " & objJob.Caption Wscript.Echo "Command: " & objJob.Command Wscript.Echo "Days Of Month: " & objJob.DaysOfMonth Wscript.Echo "Days Of Week: " & objJob.DaysOfWeek Wscript.Echo "Description: " & objJob.Description Wscript.Echo "Elapsed Time: " & objJob.ElapsedTime Wscript.Echo "Install Date: " & objJob.InstallDate Wscript.Echo "Interact with Desktop: " & objJob.InteractWithDesktop Wscript.Echo "Job ID: " & objJob.JobID Wscript.Echo "Job Status: " & objJob.JobStatus Wscript.Echo "Name: " & objJob.Name Wscript.Echo "Notify: " & objJob.Notify Wscript.Echo "Owner: " & objJob.Owner Wscript.Echo "Priority: " & objJob.Priority Wscript.Echo "Run Repeatedly: " & objJob.RunRepeatedly Wscript.Echo "Start Time: " & objJob.StartTime Wscript.Echo "Status: " & objJob.Status Wscript.Echo "Time Submitted: " & objJob.TimeSubmitted Wscript.Echo "Until Time: " & objJob.UntilTime Next |
Example of Setting a scheduled task
In this example I am going to show how to install and schedule the shutdown utility called Shutdown.exe to shut the computer down every Monday at 3:00 AM. (Click the hyperlink for assistance with that step)
- Open System Tools
- Click on Scheduled Tasks
- Double-click Add Scheduled Task
-
Click Next
- Click Browse
- Double-click the folder you downloaded Shutdown.exe to (in this case, c:\shut)
- Highlight the file
- Click Open
- Place a dot in Weekly
-
Click Next
- Set the Start time to 3:00 AM
- Place a check on Monday
-
Click Next
- Type in the password
- Retype the password for confirmation
-
Click Next
- Place a check on Open advanced properties for this task when I click Finish
-
Click Finish
- Click the tab Settings
- Uncheck Don't start the task if the computer is running on batteries
- Uncheck Stop the task if battery mode begins
- Place a check on Wake the computer to run this task
- Click OK