WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

WMI
plus
plus

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Windows NT Schedule Service - AT

 Keywords:  

Sample Code:



;***************************************************************************
;** Win32_ScheduledJob
;** The Win32_ScheduledJob WMI class represents a job scheduled using the Windows NT schedule service (AT command). 
;** This class is only instrumented on Windows NT 4.0 and later. Each job scheduled against the
;** schedule service is stored persistently (the scheduler will know to start the job even after a reboot) 
;** and is executed at the specified time and day of the week and/or month. If the computer is
;** not active or if the scheduled service is not running at the specified job time, the schedule
;** service will run the specified job on the next day at the specified time.
;** 
;** Scheduled jobs are scheduled with respect to Universal Coordinated Time (UTC) (that is, with
;** bias offset from GMT). This means that a job can be specified using any time zone specification.
;** The Win32_ScheduledJob class returns the local time with UTC offset when enumerating an object
;** and converts to local time when creating new jobs. For example, a job specified to run on a computer
;** in Boston at 10:30 P.M. Monday PST time will be scheduled to run locally at 1:30 A.M. Tuesday EST.
;** It should be noted that a client must take into account whether daylight savings time is in operation
;** on the local computer and, if so, subtract a bias of 60 minutes from the UTC offset.
;**
;** NOTE:  This only works with jobs done via AT, and will not see jobs created from the control panel scheduler.
;**
;***************************************************************************

;***************************************************************************
;** JobCreateAT(ComputerName,Command,StartTime,Repeat,DaysOfWeek,DaysOfMonth,Desktop,User,Password)
;**   Submits a job to the operating system for execution at a specified future time and date. This  
;**   method requires that the schedule service be started at the computer to which the job is submitted
;** 
;** Parameters:
;** ComputerName:	Computer name without \\, and "" for local.
;** Command:		Name of the command, batch program, or binary file (along with command line parameters) 
;**               that the schedule service will use to invoke the job. Example: "defrag /q /f". 
;** StartTime:    UTC time to run the job. This is of the form YYYYMMDDHHMMSS.MMMMMM(+-)OOO, where YYYYMMDD 
;**					must be replaced by ********. Example: ********123000.000000-420 which implies 12:30 pm PST 
;**					with daylight savings time in effect.   
;** Repeat:			Indicates whether the scheduled job should run repeatedly on the days that the job is scheduled. 
;**					Specify @TRUE to run reatedly, otherwise specify @FALSE.       
;** DaysOfWeek: 	Days of the week when the job is scheduled to run (used only when the Repeat parameter is @TRUE.)
;**					Value	Meaning 
;**					1		Monday 
;**					2		Tuesday 
;**					3		Wednesday 
;**					4		Thursday 
;**					5		Friday 
;**					6		Saturday 
;**					7		Sunday        
;** DaysOfMonth:	Days of the month when the job is scheduled to run; used only when the Repeat parameter is @TRUE.    
;** Desktop:		Indicates whether the specified job should be interactive (meaning a user can give input to the 
;**					scheduled job while it is executing). Specify @TRUE to run interactively, otherwise specify @FALSE.             
;** UserName:		User name to run program as.  In format DOMAIN\User or just User for local, 
;** 					"" for current user running script. MUST be "" for local computer or the 
;**					function will fail.
;** Password: 		Password for user or "" if username is "".
;** 
;** Returns:
;**   Returns 0 on success, and any other number to indicate an error.  
;** 
;** 
;***************************************************************************
#DefineFunction JobCreateAT(ComputerName,Command,StartTime,Repeat,DaysOfWeek,DaysOfMonth,Desktop,User,Password)
Locator = ObjectOpen("WbemScripting.SWbemLocator")
Service = Locator.ConnectServer(ComputerName,"root/cimv2",User,Password)
Security = Service.Security_
Security.ImpersonationLevel = 3
Class = Service.Get("Win32_ScheduledJob")
	Result = Class.Create(Command,StartTime,Repeat,DaysOfWeek,DaysOfMonth,Desktop)
ObjectClose(Class)
ObjectClose(Security)
ObjectClose(Service)
ObjectClose(Locator)
Return Result
#EndFunction


;***************************************************************************
;** JobGetAllAT(ComputerName,User,Password)
;**   Gets all property information about scheduled jobs.
;** 
;** Parameters:
;** ComputerName:	Computer name without \\, and "" for local.           
;** UserName:		User name to run program as.  In format DOMAIN\User or just User for local, 
;** 					"" for current user running script. MUST be "" for local computer or the 
;**					function will fail.
;** Password: 		Password for user or "" if username is "".
;** 
;** Returns:
;**   Pipe delimited list of job information about each scheduled AT job.
;** 
;** 
;***************************************************************************
#DefineFunction JobGetAllAT(ComputerName,User,Password)
Locator = ObjectOpen("WbemScripting.SWbemLocator")
Service = Locator.ConnectServer(ComputerName,"root/cimv2",User,Password)
Security = Service.Security_
Security.ImpersonationLevel = 3
Jobs = Service.InstancesOf("Win32_ScheduledJob")
hEnum = ObjectCollectionOpen(Jobs)
JobList = ""
While 1
	Job = ObjectCollectionNext(hEnum)
	If Job == 0 Then Break
	JobItem = ""
	JobCaption = job.Caption
	if JobCaption == "" then JobCaption = "NO CAPTION"
	JobItem = ItemInsert(JobCaption,-1,JobItem,@Tab)
	JobCommand = Job.Command
	if JobCommand == "" then JobCommand = "NO COMMAND"
	JobItem = ItemInsert(JobCommand,-1,JobItem,@Tab)
	JobDaysOfMonth = Job.DaysOfMonth
	if JobDaysOfMonth == "" then JobDaysOfMonth = "NO DAYSOFTHEMONTH"
	JobItem = ItemInsert(JobDaysOfMonth,-1,JobItem,@Tab)
	JobDaysOfWeek = Job.DaysOfWeek
	if JobDaysOfWeek == "" then JobDaysOfWeek = "NO DAYSOFTHEWEEK"
	JobItem = ItemInsert(JobDaysOfWeek,-1,JobItem,@Tab)
	JobDescription = Job.Description
	if JobDescription == "" then JobDescription = "NO DESCRIPTION"
	JobItem = ItemInsert(JobDescription,-1,JobItem,@Tab)
	JobElapsedTime = Job.ElapsedTime
	if JobElapsedTime=="" then JobElapsedTime="NO ELAPSEDTIME"
	JobItem = ItemInsert(JobElapsedTime,-1,JobItem,@Tab)
	JobInstallDate = Job.InstallDate
	if JobInstallDate=="" then JobInstallDate="NO INSTALLDATE"
	JobItem = ItemInsert(JobInstallDate,-1,JobItem,@Tab)
	JobItem = ItemInsert(Job.InteractWithDesktop,-1,JobItem,@Tab)
	JobJobId = Job.JobId
	if JobJobId=="" then JobJobId="NO JOBID"
	JobItem = ItemInsert(JobJobId,-1,JobItem,@Tab)
	JobJobStatus = Job.JobStatus
	if JobJobStatus == "" then JobJobStatus = "NO JOBSTATUS"
	JobItem = ItemInsert(JobJobStatus,-1,JobItem,@Tab)
	JobName = Job.Name
	if JobName == "" then JobName = "NO JOBNAME"
	JobItem = ItemInsert(JobName,-1,JobItem,@Tab)
	JobNotify = Job.Notify
	if JobNotify == "" then JobNotify = "NO NOTIFY"
	JobItem = ItemInsert(JobNotify,-1,JobItem,@Tab)
	JobOwner = Job.Owner
	if JobOwner == "" then JobOwner = "NO OWNER"
	JobItem = ItemInsert(JobOwner,-1,JobItem,@Tab)
	JobPriority = Job.Priority
	if JobPriority == "" then JobPriority = "NO PRIORITY"
	JobItem = ItemInsert(JobPriority,-1,JobItem,@Tab)
	JobRunRepeatedly = Job.RunRepeatedly
	if JobRunRepeatedly == "" then JobRunRepeatedly = "NO RUNREPEATLY"
	JobItem = ItemInsert(JobRunRepeatedly,-1,JobItem,@Tab)
	JobStartTime = Job.StartTime
	if JobStartTime == "" then JobStartTime = "NO STARTTIME"
	JobItem = ItemInsert(JobStartTime,-1,JobItem,@Tab)
	JobStatus = Job.Status
	if JobStatus == "" then JobStatus = "NO STATUS"
	JobItem = ItemInsert(JobStatus,-1,JobItem,@Tab)
	JobTimeSubmitted = Job.TimeSubmitted
	if JobTimeSubmitted == "" then JobTimeSubmitted = "NO TIMESUBMITTED"
	JobItem = ItemInsert(JobTimeSubmitted,-1,JobItem,@Tab)
	JobUntilTime = Job.UntilTime
	if JobUntilTime == "" then JobUntilTime = "NO UNTILTIME"
	JobItem = ItemInsert(JobUntilTime,-1,JobItem,@Tab)
	ObjectClose(Job)
	JobList = ItemInsert(JobItem,-1,JobList,"|")
EndWhile
ObjectCollectionClose(hEnum)
ObjectClose(Jobs)
JobList = StrTrim(JobList)
Return JobList
#EndFunction

;***************************************************************************
;** JobDeleteAllAT(ComputerName,User,Password)
;**   Deletes all AT scheduled jobs.
;** 
;** Parameters:
;** ComputerName:	Computer name without \\, and "" for local.           
;** UserName:		User name to run program as.  In format DOMAIN\User or just User for local, 
;** 					"" for current user running script. MUST be "" for local computer or the 
;**					function will fail.
;** Password: 		Password for user or "" if username is "".
;** 
;** Returns:
;**   0
;** 
;** 
;***************************************************************************
#DefineFunction JobDeleteAllAT(ComputerName,User,Password)
Locator = ObjectOpen("WbemScripting.SWbemLocator")
Service = Locator.ConnectServer(ComputerName,"root/cimv2",User,Password)
Security = Service.Security_
Security.ImpersonationLevel = 3
Jobs = Service.InstancesOf("Win32_ScheduledJob")
hEnum = ObjectCollectionOpen(Jobs)
While 1
	Job = ObjectCollectionNext(hEnum)
	If Job == 0 Then Break
	Job.Delete()
	ObjectClose(Job)
EndWhile
ObjectCollectionClose(hEnum)
ObjectClose(Jobs)
Return 0
#EndFunction


;***************************************************************************
;** 
;** 	   MAIN CODE
;** 
;***************************************************************************

ComputerName = ""
User = ""
Password = ""
Command = "notepad.exe"
; This is of the form YYYYMMDDHHMMSS.MMMMMM(+-)OOO, where YYYYMMDD must be replaced by ********.
;  Example: ********123000.000000-420 which implies 12:30 pm PST with daylight savings time in effect.
StartTime = "********123000.000000-000"
Repeat = @FALSE
DaysOfWeek = 0
DaysOfMonth = 0
Desktop = @TRUE
JobParam = StrCat("Caption",@tab,"JobItem",@Tab,"DaysOfMonth",@Tab,"DaysOfWeek",@Tab,"Description",@Tab,"ElapsedTime",@Tab,"InstallDate",@Tab,"InteractWithDesktop",@Tab)
JobParam = StrCat(JobParam,"JobId",@Tab,"JobStatus",@Tab,"Name",@Tab,"Notify",@Tab,"Owner",@Tab,"Priority",@Tab,"RunRepeatedly",@Tab,"StartTime",@Tab,"Status",@Tab,"TimeSubmitted",@Tab,"UntilTime")

; Create a task
Worked = JobCreateAT(ComputerName,Command,StartTime,Repeat,DaysOfWeek,DaysOfMonth,Desktop,User,Password)

;Get all the tasks and print them out.
JobList = JobGetAllAT(ComputerName,User,Password)

for a = 1 to ItemCount(JobList,"|")
	Job = ItemExtract(a,JobList,"|")
	line = ""
	for b = 1 to ItemCount(Job,@TAB)
		Line = StrCat(Line,ItemExtract(b,JobParam,@tab),": ",ItemExtract(b,Job,@tab),@crlf)
	Next b
	Message("Job #%a%",Line)
Next a

; Delete all the talks
Worked = JobDeleteAllAT(ComputerName,User,Password)





Article ID:   W15363
File Created: 2002:09:05:13:51:28
Last Updated: 2002:09:05:13:51:28