Scripting and scheduled tasks.....

Scripting and scheduled task...

I wanted to find a an easy way to create and check scheduled tasks via a script, not as easy as I first thought - all will be revealed why, read on.

so I create a scheduled task via the scheduled task control panel applet, no problem there.
I then try and query it via a vb script using WMI (attached) nothing is returned, no scheduled task present. Even using other WMI tools and looking in CIMV2 Win32_ScheduledJob - no jobs there.

However when I create a scheduled task via a vb script (see below)

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" _
    & strComputer & "\root\cimv2")
JobID = "Test"
Set objNewJob = objWMIService.Get("Win32_ScheduledJob")
errJobCreate = objNewJob.Create _
    ("Notepad.exe", "********143000.000000-420", _
        True , 1 OR 4 OR 16, ,True, JobId)
If errJobCreate = 0 Then
    WScript.Echo "Job created successfully: " & VBNewLine _
        & "Notepad.exe scheduled to run repeately at 14.30 (2:30 P.M.) PST" & VBNewLine _
        & "on Mon, Wed, and Fri."
Else
    WScript.Echo "Job not created. Error code = " & errJobCreate
End If

And query the scheduled tasks via a script, the query returns the script generated scheduled task, the reason (I have now found out) is below.

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. If you do that, you can then use a script to retrieve information about that task and about any other tasks created using the At APIs:

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.

Filed under: ,