How Can I Execute a Scheduled Task in Windows Vista?


Hey, Scripting Guy! Question

Hey, Scripting Guy! I understand that, in Windows Vista, I can write a script that will cause a scheduled task to immediately execute. How do I do that?

— KW

SpacerHey, Scripting Guy! AnswerScript Center

Hey, KW. Before we answer today’s question, we’d like to take a moment to say a few things in memory of our colleague, Scripting Guy Dean Tsaltas. Not that Dean has died or anything; physically he’s in great shape. (Well, OK: physically he’s in as good a shape as he ever is.) No, as it turns out, Dean has suffered a fate far worse than death: he and his family are moving back to Halifax, Nova Scotia.

Sample Nova Scotian humor. Did you hear about the war between Newfoundland and Nova Scotia? The Newfies were lobbing hand grenades; the Nova Scotians were pulling the pins and throwing them back.

OK, well, we assume this joke is like curling: you probably have to be Canadian to truly appreciate it.

Now, for those of you who worry about things like this, don’t worry: Dean was, is, and always will be a Scripting Guy. In addition, Dean is still working for Microsoft, and he’s working on the WMI SDK; the only difference is that he’ll be doing this work in Halifax, where you’re much more likely to find a slice of Rappie Pie than you are here in Redmond.

Nova Scotian recipe. One traditional recipe for Rappie Pie includes, among other things, the following ingredients: 2 pounds of fatty pork; ¼ pound of pork fat; ¼ pound of fried salt port. And if you get tired of Rappie Pie (as if!), well, in that case you can always run down to the grocery store and get a box of salted codfish. As the name implies, salted codfish is nothing more than codfish that has been immersed in salt and then dried in the sun. Do those Nova Scotians know how to live or what?

At any rate, we Scripting Guys would like to take a moment to pay tribute to Dean and all the many wonderful things he’s done for the Script Center and for the scripting world at large. Unfortunately, however, while we’d like to take a moment to pay tribute to Dean and all the many wonderful things he’s done for the Script Center and the scripting world at large, we couldn’t actually think of anything he’s done, wonderful or otherwise. But, then again, he only worked here for six years.

Oh, wait, we just thought of something: Dean did get us all in trouble when he said the word “pee” during one of our Scripting Week 1 webcasts. And he did write the Scriptomatic, the tool that, more than anything else, helped bring WMI into the world of system administration. And if that wasn’t enough, you could also count on Dean to go along whenever it was time for lunch; in fact, Dean would happily take two or three lunches a day if necessary.

And once Dean is gone, how will the rest of ever know what’s going on in the world of Ultimate Fighting?

In other words, this is a sad day for the Scripting Guys. But things will work out: after all, Dean might be gone, but he won’t be forgotten. Instead, he’ll remain foremost in our minds, the very same way that … um … that the … uh … other Scripting Guy, the one who became a PM or something, has remained foremost in our minds.

Besides, there’s nothing to prevent all of us from going to Halifax to visit Dean, is there?

Nova Scotian Weather Report. In January, 1993 the temperature at the Halifax Airport reached a low of -28.5 degrees Celsius (-19.3 degress Fahrenheit). On February 19, 2004, the airport was inundated with 66 centimeters (26 inches) of snow.

Of course, sad day or not, duty calls. With that in mind, here’s a little farewell gift to Dean from his fellow Scripting Guys; a script that, under Windows Vista, can immediately execute a scheduled task:

Set objTaskService = CreateObject(“Schedule.Service”)

Set objRootFolder = objTaskService.GetFolder(“\”) Set objTask = objRootFolder.GetTask(“Test Task”)

objTask.Run vbNull

If this looks like a pretty simple little script, well, there’s a good reason for that: it is a pretty simple little script. As you can see, we start out by creating an instance of the Schedule.Service object, and then call the Connect method; that connects us to the Task Scheduler service on the local computer. Can we perform this same task on a remote computer, say, a computer named atl-fs-01? You bet we can; all we have to do is pass the name of that computer to the Connect method, like so:


After connecting to the Task Scheduler service, we use the GetFolder method to bind to the Task Scheduler’s root folder:

Set objRootFolder = objTaskService.GetFolder(“\”)

What’s that? What if your task is in a subfolder of the root folder; you know, a folder with a name like Admin Tasks? No problem; just specify the complete path to the folder, like so:

Set objRootFolder = objTaskService.GetFolder(“\Admin Tasks”)

Once we connect to the proper folder we can then call the GetTask method to create an object reference to the desired task; to do this, we simply pass GetTask the name of the task we want to run:

Set objTask = objRootFolder.GetTask(“Test Task”)

And then to run the task we just have to call the Run method, passing the VBScript constant vbNull as a way to indicate that we want to run the task without any special parameters:

objTask.Run vbNull

Just like that, the scheduled task Test Task will run.

Note. You’re absolutely right: the Task Scheduler in Windows Vista is way cooler – and way more scriptable – than the Task Scheduler in other versions of Windows. For a quick introduction to writing scripts that interact with the Windows Vista Task Scheduler take a peek at our article Task Scheduler: Part 1.

And just like that, we’re done for today. We’ll be back tomorrow, although with a heavy heart: tomorrow is Dean’s last day. But to tell you the truth, between Rappie Pie, salted codfish, and the Duck Trolling Retriever (the official dog of the province of Nova Scotia), it’s amazing to us that everyone hasn’t decided to move to Nova Scotia. Good luck, Dean, and remember: although you might be in Halifax, there will always be a place for you here in the Script Center.

Well, as long as you don’t use the word “pee.” We’d just as soon not go through all that again.


Discussion is closed.

Feedback usabilla icon