Enumerating Scheduled Tasks

Microsoft® Windows® 2000 Scripting Guide

microsoft windows 2000 scripting guide

« Previous | Next »   

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.

Table 17.13   Win32_ScheduledJob Properties

PropertyDescription
CaptionShort description (one-line string) of the task.
CommandName 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"
DaysOfMonthDays 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

DaysOfWeekDays 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

DescriptionDescription of the object.
ElapsedTimeLength of time that the job has been executing.
InstallDateDate the job was created.
InteractWithDesktopBoolean value indicating that the specified job is interactive (meaning a user can give input to a scheduled job while it is executing).
JobIDIdentifier number of the scheduled task.
JobStatusStatus of execution the last time this job was supposed to run. Values are:

Success

Failure

NotifyUser to be notified upon job completion or failure.
OwnerUser that submitted the job.
PriorityUrgency or importance of execution of a job.
RunRepeatedlyScheduled job should run repeatedly on the days that the job is scheduled. If FALSE, the job is run once.
StartTimeUTC 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.

TimeSubmittedTime that the job was created.
UntilTimeTime 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:

  1. Create a variable to specify the computer name.
  2. Use a GetObject call to connect to the WMI namespace root\cimv2 on the computer, and set the impersonation level to "impersonate."
  3. 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.

  4. 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

send us your feedback Send us your feedback « Previous | Next »   


Microsoft Windows 2000 Scripting Guide(c) Automating System Administration 2003
Microsoft Windows 2000 Scripting Guide(c) Automating System Administration 2003
ISBN: N/A
EAN: N/A
Year: 2004
Pages: 635

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net