Flylib.com

Books Software

 
 
 

Recipe 10.13. Viewing Scheduled Tasks


Recipe 10.13. Viewing Scheduled Tasks

Problem

You want to view the list of scheduled tasks on a system.

Solution

Using a graphical user interface

From the Control Panel, open the Scheduled Task applet.

Using a command-line interface

The following command lists the scheduled tasks:

> schtasks /query

To get detailed information about each task, run the following command:

> schtasks /query /v /fo list

Using VBScript
' This code lists the scheduled AT tasks on a computer.

' ------ SCRIPT CONFIGURATION ------

strComputer = "


<HostName>


"

' ------ END CONFIGURATION ---------

set objWMI = GetObject("winmgmts:\" & strComputer & "\root\cimv2")

set colScheduledJobs = objWMI.ExecQuery("Select * from Win32_ScheduledJob")

for each objJob in colScheduledJobs

   WScript.Echo "Job ID: " & objJob.JobID

   for each objProp in objJob.Properties_

       WScript.Echo "  " & objProp.Name & ": " & objProp.Value

   next 

next

Discussion

Another quick way to view the scheduled tasks on a system is to simply browse the Scheduled Tasks share point on the system ( \\ <HostName> \Scheduled Tasks ). When you create a scheduled task, a job file is created that contains the settings for the task, which is placed in %SystemRoot%\Tasks . This directory is shared out as Scheduled Tasks . Unfortunately, the job files are stored in a binary format, so you cannot simply create or modify them with a text editor. You can, however, copy and paste jobs between machines. If you want to copy a job from HostA to HostB, open the Scheduled Tasks share point on both servers, right-click on the target job on HostA and select Copy, then paste the copy into the Scheduled Tasks share of HostB. Make sure any localized settings in the task are modified on HostB after the copy is complete.

See Also

Recipe 10.12 for creating a scheduled task; Recipe 10.14 for deleting a scheduled task; MS KB 310424, "HOW TO: Work with Scheduled Tasks on Remote Computers in Windows XP"


Recipe 10.14. Deleting a Scheduled Task

Problem

You want to delete a scheduled task.

Solution

Using a graphical user interface

  1. From the Control Panel, open the Scheduled Task applet.

  2. Right click on the target task and select Delete.

  3. Click Yes to confirm.

Using a command-line interface

You can use the schtasks .exe command to delete a task. The following command deletes the task named Job1 :

> schtasks /delete /tn Job1

Using Group Policy

You can't delete a task with Group Policy, but you can prevent it from being deleted. Here is where you can find that setting:

\Computer Configuration\

    Administrative Templates\Windows Components\Task Scheduler

\User Configuration\

    Administrative Templates\Windows Components\Task Scheduler



Prohibit Task Deletion

Using VBScript
' This code deletes a scheduled AT task.

' ------ SCRIPT CONFIGURATION ------

intJobID =


<JID>


' e.g. 1452

strComputer = "


<HostName>


"   ' e.g. dns01 

' ------ END CONFIGURATION ---------

set objWMI = GetObject("winmgmts:\" & strComputer & "\root\cimv2")

Set objInstance = objWMI.Get("Win32_ScheduledJob.JobID=" & intJobID)

intRC = objInstance.Delete

if intRC <> 0 then

   Wscript.Echo "Failure deleting task id: " & intJobID

else 

   Wscript.Echo "Sucessfully deleted task id: " & intJobID

end if

Discussion

Using a graphical user interface

You can't use the applet to delete a task on a remote system. However, there is another option. By default, a Scheduled Tasks share is created on Windows XP, which contains the job files for each scheduled task. Simply browse to \\ <HostName> \ Scheduled Tasks and you should see the list of scheduled tasks on that system (if you have administrator privileges). From here you can right-click a task and select Delete.

Using a command-line interface

To delete a task from the command line, you need to know the task name. Unless you know it off the top of your head, you'll need to query the current scheduled tasks to find the name of the one you want to delete. See Recipe 10.13 for more on how to do that.

See Also

Recipe 10.9 for creating a scheduled task; Recipe 10.13 for listing the scheduled tasks; and MS KB 310424, "HOW TO: Work with Scheduled Tasks on Remote Computers in Windows XP"