How Can I Manage Scheduled Tasks Using Scripts?

How Can I Manage Scheduled Tasks Using Scripts?

  • Comments 5
  • Likes
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.


Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • <p>Is there a script that will switch on a scheduled task</p>

  • <p>The reason I visited this page is that I am seeking, apparently in vain, any way to run a Powershell script in XP that can see the tasks on a Windows Server 2008 server.</p> <p>In addition to the differences that this article describes (between AT/WMI and Task Scheduler), guess what. &nbsp;The SCHTASKS utility in XP cannot remotely read the scheduled tasks in Windows Server 2008 machines.</p> <p>Not only did the difference not get resolved but now we have THREE different schemes to worry about: AT/Win32, Task Scheduler in XP/W2K and Task Scheduler in W2K8. &nbsp;Oh boy!</p>

  • <p>@Blag I have a whole series of PowerShell scripts that work with the task scheduler. Take a look at those articles. </p> <p>@Harv Stewart This article is about VBScript. I have a whole series of Hey Scripting Guy! articles that &nbsp;talk about the different job schedule engines, and show using Windows PowerShell to work with these tasks. I specifically talk about using scheduled tasks to call both PowerShell commands, and to launch PowerShell scripts. If the link is hard to read, go to <a rel="nofollow" target="_new" href="http://www.ScriptingGuys.com">http://www.ScriptingGuys.com</a> and use the PowerShell and the Scheduled Tasks tags from the tag cloud. Here is the exact link. <a rel="nofollow" target="_new" href="http://blogs.technet.com/b/heyscriptingguy/archive/tags/windows+powershell/scheduled+tasks/">blogs.technet.com/.../scheduled+tasks</a> </p>

  • i am getting error code 2 ,what i have to do ?

  • I need to run a a program on Daily basis, what should i write there? 1 OR 2 OR 4 OR 8 OR 16 OR 32 OR 64 or something else. Please suggest