This example demonstrates the creation of a simple job.
The job created in this example:
This example shows the minimum amount of code needed to create a simple job. Most properties are left set to their default values. No error handling is provided.
The method requires an existing client connection. See Creating a Client Session for an example of creating a client connection.
Public Sub CreateJob(ByVal theScheduler As Scheduler) Dim job1 As Job Dim step1 As JobStep Dim progex As ProgramExecutionTask Dim trigger As ScheduleTrigger Dim schedule1 As Schedule Dim dateschedule1 As DateSchedule Dim timecriterion As SpecifyTimeCriterion Dim datecriterion As DayIntervalCriterion Dim errors As New ADErrors Dim errorSeverity As ADErrorSeverity Dim ipv As IProvideValidation 'Data objects (objects derived from IADTObject) cannot be directly created. 'They must be created through a call to Scheduler.CreateObject() job1 = CType(theScheduler.CreateObject(ClassIDEnum.CID_Job), Job) job1.Name = "API Sample 1" 'See the Credential Profile Retrieval example for the implementation of CredentialProfileGetter.GetCredentialProfile 'replace with the appropriate user ID and password. job1.Credentials = CredentialProfileGetter.GetCredentialProfile(theScheduler, "mydomain\myuserID", "mypassword") 'By default, the job will run in a hidden logon session and will not be visible 'to any interactive users 'we want the job to be visible to the user when it is run 'The settings here tell adTempus to run the job in the user's logon session, if 'the user is logged on, or in the Console session if the user is not logged on, 'or to fail if neither of these is available job1.LogonSessionOptions = LogonPreferenceEnum.Interactive1 Or LogonPreferenceEnum.Console2 'we'll use the defaults for all other Job properties 'next we need a step step1 = CType(theScheduler.CreateObject(ClassIDEnum.CID_JobStep), JobStep) 'and a task. Use a ProgramExecutionTask to run a program progex = CType(theScheduler.CreateObject(ClassIDEnum.CID_ProgramExecutionTask), ProgramExecutionTask) 'we'll run Notepad.exe (%windir% will be expanded by adTempus 'to the value of the WINDIR environment variable) progex.ExecutionTarget = "%windir%\system32\notepad.exe" progex.WindowMode = WindowModeEnum.wmNormal 'we want it to be visible 'assign the task to the step step1.Task = progex 'add the step to the job job1.Steps.Add(step1) 'next we need a trigger 'we'll schedule this to run at 12:05 PM every day 'add a single time (12:05 PM) to the time criterion timecriterion = CType(theScheduler.CreateObject(ClassIDEnum.CID_SpecifyTimeCriterion), SpecifyTimeCriterion) timecriterion.Add(#12:05:00 PM#) 'set the date criterion to execute every day datecriterion = CType(theScheduler.CreateObject(ClassIDEnum.CID_DayIntervalCriterion), DayIntervalCriterion) datecriterion.Interval = 1 'assign the date criterion to the date schedule dateschedule1 = CType(theScheduler.CreateObject(ClassIDEnum.CID_DateSchedule), DateSchedule) dateschedule1.DateCriterion = datecriterion 'add the date and time criteria to the schedule schedule1 = CType(theScheduler.CreateObject(ClassIDEnum.CID_Schedule), Schedule) schedule1.DateSchedule = dateschedule1 schedule1.TimeCriterion = timecriterion 'add the schedule to the trigger trigger = CType(theScheduler.CreateObject(ClassIDEnum.CID_ScheduleTrigger), ScheduleTrigger) trigger.Schedules.Add(schedule1) 'and add the trigger to the job job1.Triggers.Add(trigger) 'before we save the job we'll validate it 'Saving automatically validates first, but if Save returns validation errors, 'we'd have to call Validate 'anyway to get details of the error 'first cast the job to an IProvideValidation interface ipv = CType(job1, IProvideValidation) errorSeverity = ipv.Validate(errors) If errorSeverity > ADErrorSeverity.adetWarning Then 'one or more errors were reported. 'examine the contents of the errors collection for details Else 'validation was successful. Save the job. job1.Save() End If End Sub
adTempus API Reference version 3.0.0.0, revised 10/30/2008
|