I needed to automate creation of a job last night on a Windows computer. I was able to find examples of such a script, but realized that they lack a very important part - the ability to schedule a job taking into consideration the Time Zone the computer is in. This is important as the VBScript needs the time zone offset to schedule the script accurately. For people residing in different time zones the offset value will be different and this is also a factor for scheduling a job on computers in different time zones. I thought why not to overcome this limitation and make scheduling jobs easier? I was able to craft such a script. I think the time zone alignment is a necessary part of any job scheduling script. The code is below:
'Purpose: Schedule a Windows Scheduler Task
'Notes..: The script is capable of adjusting itself to the time zone of the computer
Dim offset
Dim strComputer
Dim errJobCreated
'Use local machine
strComputer = "."
For Each os In GetObject("winmgmts:").InstancesOf ("Win32_OperatingSystem")
offset = CStr(os.CurrentTimeZone)
Next
'Schedule notepad to run at 12:30 pm every other work day
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set objNewJob = objWMIService.Get("Win32_ScheduledJob")
errJobCreated = objNewJob.Create _
("Notepad.exe", "********123000.000000" & offset , _
True , 1 OR 4 OR 16, , , JobID)
If errJobCreated = 0 Then
Wscript.Echo "Scheduled task has been successfully created."
Else
Wscript.Echo "New scheduled task creation has FAILED!."
End IF