Question : Scheduled Task creation with VBscript

I want to create a scheduled task that runs from a different user account. How do I specify the user account for it to run under and the password of that account. I also want it to run everyday at 12:30 A.M.

Answer : Scheduled Task creation with VBscript

Hi ginnis24,

Under Windows XP there are two independent task schedulers.  One is the AT.exe commandline scheduler which hails from NT4 days, and one is the scheduler most people are familiar with under Control Panel / Scheduled Tasks.

The AT.exe scheduler has a WMI clas that you can use.  This is described here (http://www.microsoft.com/technet/scriptcenter/resources/qanda/sept04/hey0922.mspx) and it also explains a little of the differences between the two methods.

In XP, there is no way to properly script directly the Control Panel / Scheduled Tasks. In VISTA you can using the following:
Set objTaskService = CreateObject("Schedule.Service")
objTaskService.Connect

SO - the best way I have found is to shell out to use the XP commandline utility SCHTASKS.exe.

The command to create a task is fully exposed by opening a command prompt and typing 'SCHTASKS /Create /?'

e.g. one example may be as follows.  This command will schedule the command C:\scripts\Hello.cmd to run as System account on computer SHARONPC1 to run on startup.  The task will be called "Start up task - Hello":

SCHTASKS /Create /S SHARONPC1 /RU System /SC ONSTART /TN "Start up task - Hello" /TR "C:\scripts\Hello.cmd"


To execute that from vbScript you can adapt the lines below.  Note that when shelling out you need to double up all double-quotes that are within the string.

Hope this helps,
Daz.
1:
2:
3:
4:
Set wshShell = CreateObject("WScript.Shell")
ret = wshShell.Run ("SCHTASKS /Create /S SHARONPC1 /RU System /SC ONSTART /TN ""Start up task - Hello"" /TR ""C:\scripts\Hello.cmd"""
, 1, True)
MsgBox "Scheduling of task complete.  Return code from SCHTASKS.exe = " & ret
Random Solutions  
 
programming4us programming4us