Export (0) Print
Expand All
3 out of 3 rated this helpful - Rate this topic

Creating Scheduled Tasks

Microsoft® Windows® 2000 Scripting Guide

To run a program or script at a specified time on a specified date, you must create a scheduled task that contains the following information:

  • Path to the program or script to be run.

  • Days of the week on which the program or script is to be run.

  • Time of day at which the program or script is to be run.

Scheduled tasks can be created on local computers by using the Scheduled Task Wizard. However, this wizard cannot be used to schedule tasks on remote computers (or to simultaneously schedule tasks on more than one computer). If you need to create scheduled tasks on remote or multiple computers, you can use the command-line tool At.exe or create a custom script by using the WMI class Win32_ScheduledJob.

The Win32_ScheduledJob Create method can be used to create scheduled tasks on a computer. The Create method requires the parameters shown in Table 17.14. Parameters not required for a specific task should be left blank.

Table 17.14 Win32_ScheduledJob Properties Available to the Create Method

Property

Description

Command

Name of the executable program, batch file, or script to be run.

StartTime

UTC time to run the job. This is of the form YYYYMMDDHHMMSS.MMMMMM(+-)OOO, where YYYYMMDD must be replaced by ********. Example: ********123000.000000-420, which implies 12:30 P.M. Pacific time with daylight saving time in effect.

RunRepeatedly

Indicates whether the scheduled job should run repeatedly on the days that the job is scheduled. The default is FALSE. This parameter is optional.

DaysOfWeek

Days of the week when the task should be run. Values are:

1 - Monday

2 - Tuesday

4 - Wednesday

8 - Thursday

16 - Friday

32 - Saturday

64 - Sunday

To run the task on multiple days, use a logical OR to combine values. For example, to run a task on Tuesday, Thursday, and Saturday, use the following code:

2 OR 8 OR 32

To run a task on multiple days, you must set the RunRepeatedly parameter to True.

DaysOfMonth

Days of the month when the job is scheduled to run; used only when the RunRepeatedly parameter is True. This parameter is optional.

InteractWithDesktop

Indicates whether the specified job should be interactive (meaning that a user can give input to the scheduled job while it is executing). The default is False. This parameter is optional and is rarely set to True. In general, the reason to use a scheduled task is to allow a task to be carried out without any user interaction.

JobID

Identifier number of the job. This parameter is a handle to the job being scheduled on this computer.

When a scheduled task is created, one of the error codes shown in Table 17.15 is returned, indicating the success or failure of the procedure.

Table 17.15 Win32_ScheduledJob Error Codes

Error Code

Description

0

The scheduled task was successfully created.

1

The request is not supported.

2

The user did not have the necessary access.

8

Interactive Process.

9

The directory path to the service executable file was not found.

21

Invalid parameters have been passed to the service.

22

The account under which this service is to run either is invalid or lacks the permissions to run the service.

Scripting Steps

Listing 17.30 contains a script that schedules a task on a computer. To carry out this task, the script must perform the following steps:

  1. Create the constant RUN_REPEATEDLY and set the value to True.

    This constant will be used to indicate that the scheduled task should run repeatedly. If set to False, the scheduled task will run once and then never run again.

  2. Create the constants MONDAY, WEDNESDAY, and FRIDAY and set the respective values to 1, 4, and 16.

    These constants represent the days of the week when the script is scheduled to run.

  3. Use a GetObject call to connect to the WMI service.

  4. Retrieve an instance of the Win32_ScheduledJob class.

  5. Call the Create method, specifying the following:

    • Name of the executable file or script to be scheduled.

      In this example, the file is Monitor.exe.

    • The time that the task is scheduled to run (12:30 P.M.) in UTC format. The eight asterisks indicate that the year, month, and day are irrelevant. Only the time itself (123000, meaning 12:30:00) is important.

    • Whether the job should run repeatedly or just once.

      The constant RUN_REPEATEDLY, with the value True, means that the job should run repeatedly.

    • The days of the week on which the job should run.

      The constants MONDAY, WEDNESDAY, and FRIDAY indicate the days of the week when the task should run.

    • The days of the month on which the job should run.

      This parameter is left blank because the job is scheduled to run on specific days of the week.

    • Whether the task needs to interact with the user.

      This parameter is left blank because the task does not need to interact with the user.

    • The variable name (JobID) that will hold the identification number assigned to the new job.

  6. If the operation succeeded, echo the Job ID assigned to the new task.

Listing 17.30 Scheduling a Task

  
1
2
3
4
5
6
7
8
9
10
11


strComputer = "."
Set objService = GetObject("winmgmts:\\" & strComputer)
Set objNewJob = objService.Get("Win32_ScheduledJob")
errJobCreated = objNewJob.Create _
 ("Monitor.exe", "********123000.000000-420", _
 True , 1 OR 4 OR 16, , , JobID)
If Err.Number = 0 Then
 Wscript.Echo "New Job ID: " & JobID
Else
 Wscript.Echo "An error occurred: " & errJobCreated
End If

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.