Deleting Scheduled Tasks

Microsoft® Windows® 2000 Scripting Guide

microsoft windows 2000 scripting guide

« Previous | Next »   

When a scheduled task is no longer needed on a computer, it should be deleted. If a task is not deleted, it will continue to run as scheduled. This can create problems that might be a minor irritant (running a monitoring program whose data is no longer needed) or that might be extremely destructive (deleting files and folders that should not be deleted).

Scheduled tasks can be deleted using the Win32_ScheduledJob Delete method. There are two ways to delete scheduled tasks:

  • Delete a single scheduled task. To delete a single scheduled task, your script needs to connect to the specific task (using the JobID as a unique identifier) and then call the Delete method.
  • Delete all the scheduled tasks on a computer. To delete all the scheduled tasks on a computer, your script needs to retrieve a list of all the scheduled tasks, connect to each one, and then individually delete each task.

Scripting Steps

There are multiple ways to delete scheduled tasks:

  • Deleting a single scheduled task.
  • Deleting all the scheduled tasks on a computer.

Deleting a single scheduled task

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

  1. Use a GetObject call to connect to the WMI service.
  2. Retrieve the instance of the Win32_ScheduledJob class where the JobID = 1.

    JobID is a unique identifier for the specific task. The file name in this case is At1.job.

  3. Use the Delete method to delete the job.

Listing 17.31   Deleting a Single Scheduled Task

1 2 3 4 
strComputer = "." Set objService = GetObject("winmgmts:\\" & strComputer) Set objInstance = objService.Get("Win32_ScheduledJob.JobID=1") objInstance.Delete

Deleting all the scheduled tasks on a computer

Listing 17.32 contains a script that deletes all the scheduled tasks on a computer. To carry out this task, the script must perform the following steps:

  1. Use a GetObject call to connect to the WMI service.
  2. Use a second GetObject call to connect to the Win32_ScheduledJob class.
  3. For each scheduled job, store the JobID in the variable intJobID.
  4. Connect to the individual job, specifying the value of intJobID as the JobId.

    Because the Delete method will not work on a collection, you must individually connect to each scheduled task and then use the Delete methods to remove the tasks one by one.

  5. Use the Delete method to delete the scheduled task.

Listing 17.32   Deleting All the Scheduled Tasks on a Computer

1 2 3 4 5 6 7 8 9 10 
strComputer = "." Set objService = GetObject("winmgmts:\\" & strComputer) Set colScheduledTasks = objService.ExecQuery _     ("SELECT * FROM Win32_ScheduledJob") For Each objTask in colScheduledTasks     intJobID = objTask.JobID     Set objInstance = objService.Get_         ("Win32_ScheduledJob.JobID=" & intJobID & "")     objInstance.Delete 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