How Can I Manage Scheduled Tasks Using Scripts?

Hey, Scripting Guy! Question

Hey, Scripting Guy! Is there an easy way to manage scheduled tasks using scripts?

— RC

SpacerHey, Scripting Guy! AnswerScript Center

Hey, RC. You just had to ask that question, didn’t you? To be honest, this is not one of scripting’s strong points, and you might be more confused after hearing the answer than you were before asking the question. But here goes.


The problem we have here is that Windows actually has two different – and, sadly, not fully-compatible – APIs (Application Programming Interfaces) used to manage scheduled tasks. For one, there are the Task Scheduler APIs used by both the Task Scheduler and by Schtasks.exe, a command-line task management tool that ships with Windows XP and Windows Server 2003. For another, there are the so-called At APIs used by At.exe and by WMI’s Win32_ScheduledJob class.


So what’s the problem? Well, let’s say you use WMI to create a scheduled task (and we’ll show you a sample script for doing so in a minute). If you do that, you can then use a script similar to this to retrieve information about that task and about any other tasks created using the At APIs:

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

On top of that, if you open up the Task Scheduler, you’ll see the task in there as well. So far so good, huh?


However, suppose you say to yourself, “Hey, as long as I have the Task Scheduler open I might as well schedule a second task.” Let’s say you do so. That task, created by the Task Scheduler, will not be visible to your WMI scripts. The Task Scheduler can deal with tasks created by WMI, but WMI cannot deal with tasks created by the Task Scheduler.


Even worse, suppose you use Task Scheduler to modify the task you originally created with WMI. That task will be successfully modified, but you’ll no longer be able to access it using WMI, even though WMI created the thing in the first place. Yes, we know. And, hopefully, this problem will be corrected in future versions of Windows. And, yes, we know: that doesn’t really help you much right now, does it? Sorry.


So what does all this mean? Well, it means that managing scheduled tasks using WMI is kind of a hit-or-miss proposition. If you can be assured that no one is scheduling tasks using the Task Scheduler then WMI works great. But if people are using both WMI scripts and the Task Scheduler, well, then management becomes a bit more complicated, to say the least.


So how do you create a scheduled task using a script? Try this example for starters:

strComputer = “.”
Set objWMIService = GetObject(“winmgmts:” _
& “{impersonationLevel=impersonate}!\\” & strComputer & “\root\cimv2”)
Set objNewJob = objWMIService.Get(“Win32_ScheduledJob”)
errJobCreated = objNewJob.Create _
(“Cleanup.exe”, “********123000.000000-420”, _
True , 1 OR 4 OR 16, , , JobID)
Wscript.Echo errJobCreated

A bit cryptic, isn’t it? The preceding script schedules a hypothetical program named Cleanup.exe to run at 12:30 PM every Monday, Wednesday, and Friday. For more information about the different parameters used in this script, you might want to check out this section of the Microsoft Windows 2000 Scripting Guide.