| Imports ADTempus |
| Imports System.IO |
| |
| |
| Namespace AdTempusAPI |
| |
| |
| Module Module1 |
| |
| Sub Main() |
| |
| 'Dim dayspecs As DaySpecification |
| Dim jobTime As Date |
| Dim theScheduler As scheduler |
| Dim theApp As Application |
| 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 daycriterion As SpecifyDaysCriterion |
| Dim datecriterion As DayIntervalCriterion |
| 'Dim theresponse As Response |
| Dim newresponse As Response |
| Dim eventFilter As JobEventFilter |
| Dim thenotificationaction As NotificationAction |
| Dim newrecipient As NotificationIndividual |
| 'Dim restartAction As JobControlAction |
| |
| |
| |
| |
| |
| 'Dim errors As ArcanaDevelopment.ADErrorHandling.ADErrors |
| 'Dim errorSeverity As ADErrorSeverity |
| |
| ' Loop through each line in array returned by ReadAllLines |
| Dim line As String |
| For Each line In File.ReadAllLines("C:\inputs.csv") |
| |
| ' Split line on comma |
| Dim jobLine As String() = line.Split(New Char() {","c}) |
| |
| |
| Dim ipv As IProvideValidation |
| 'create an application object |
| theApp = New Application |
| |
| 'create a new session using native Windows security |
| theScheduler = theApp.Connect("", "") |
| |
| 'Data objects (objects derived from IADTObject) cannot be directly created. |
| 'They must be created through a call to scheduler.CreateObject() |
| job1 = theScheduler.CreateObject(ClassIDEnum.CID_Job) |
| |
| job1.Name = jobLine(0) |
| |
| 'replace with the appropriate credentials |
| job1.UserID = jobLine(1) |
| job1.Password = jobLine(2) |
| |
| 'we'll use the defaults for all other Job properties |
| |
| |
| 'next we need a step |
| step1 = theScheduler.CreateObject(ClassIDEnum.CID_JobStep) |
| |
| 'and a task. Use a ProgramExecutionTask to run a program |
| progex = theScheduler.CreateObject(ClassIDEnum.CID_ProgramExecutionTask) |
| |
| 'we'll run Notepad.exe (%windir% will be expanded by adTempus |
| 'to the value of the WINDIR environment variable) |
| progex.ExecutionTarget = jobLine(3) |
| progex.CommandLineParameters = jobLine(4) |
| progex.WindowMode = WindowModeEnum.wmNormal 'we want it to be visible |
| |
| '!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! |
| '*************************************************************************************************************** |
| '!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! |
| 'we want to look at the exit code to determine success |
| progex.SuccessCriterion = SuccessCriterionEnum.scExitCode |
| 'success is indicated by an exit code <= 4 |
| progex.ExitCodeCriterion = ExitCodeTestTypeEnum.exitCodeLE |
| progex.SuccessMinCode = 4 |
| |
| 'if the task fails, we want to retry it. |
| 'create the response |
| 'theresponse = theScheduler.CreateObject(ClassIDEnum.CID_Response) |
| |
| 'create an event filter to trigger the response when the step fails() |
| 'eventFilter = theScheduler.CreateObject(ClassIDEnum.CID_JobEventFilter) |
| 'eventFilter.Event = JobEventEnum.jeStepFailed |
| 'theresponse.Events.Add(eventFilter) |
| |
| 'now the action to restart |
| 'restartAction = theScheduler.CreateObject(ClassIDEnum.CID_JobControlAction) |
| 'restartAction.ControlType = JobControlActionsEnum.jcaRestartStep |
| 'restartAction.RestartDelay = 120 'wait 120 seconds (=2 minutes) before trying again() |
| 'restartAction.RetryLimit = 4 'retry up to 4 times |
| |
| 'theresponse.Actions.Add(thenotificationaction) |
| 'step1.Responses.Add(theresponse) |
| |
| 'we want to send e-mail notification if the task still doesn't succeed after the 4 retries. |
| 'first create the recipient. This method will retrieve the existing |
| 'recipient object if one exists, or create a new one if necessary |
| |
| newrecipient = GetRecipient("[email protected]", theScheduler) |
| |
| 'create another response |
| newresponse = theScheduler.CreateObject(ClassIDEnum.CID_Response) |
| |
| 'create an event filter to trigger the response when the restart limit Is exceeded |
| eventFilter = theScheduler.CreateObject(ClassIDEnum.CID_JobEventFilter) |
| eventFilter.Event = JobEventEnum.jeStepFailed |
| newresponse.Events.Add(eventFilter) |
| |
| 'create the action to send the notification |
| thenotificationaction = theScheduler.CreateObject(ClassIDEnum.CID_NotificationAction) |
| thenotificationaction.Recipients.Add(newrecipient) |
| 'we'll use the default subject and message, so we don't need to do anything else with the action |
| |
| newresponse.Actions.Add(thenotificationaction) |
| step1.Responses.Add(newresponse) |
| |
| '!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! |
| '*************************************************************************************************************** |
| '!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! |
| |
| '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 = theScheduler.CreateObject(ClassIDEnum.CID_SpecifyTimeCriterion) |
| jobTime = Date.Parse(jobLine(6)) |
| timecriterion.Add(jobTime) |
| |
| 'set the date criterion to execute every day |
| datecriterion = theScheduler.CreateObject(ClassIDEnum.CID_DayIntervalCriterion) |
| datecriterion.Interval = jobLine(5) |
| |
| |
| 'setting the date specification to run on MTWRF only |
| 'daycriterion = theScheduler.CreateObject(ClassIDEnum.CID_DaySpecification) |
| 'dayspecs.WeekdayMask = 111110 |
| |
| 'assign the date criterion to the date schedule |
| dateschedule1 = theScheduler.CreateObject(ClassIDEnum.CID_DateSchedule) |
| dateschedule1.DateCriterion = datecriterion |
| |
| 'add the date and time criteria to the schedule |
| schedule1 = theScheduler.CreateObject(ClassIDEnum.CID_Schedule) |
| schedule1.DateSchedule = dateschedule1 |
| schedule1.TimeCriterion = timecriterion |
| |
| 'add the schedule to the trigger |
| trigger = theScheduler.CreateObject(ClassIDEnum.CID_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 = job1 |
| '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 |
| |
| Next |
| |
| |
| End Sub |
| Private Function GetRecipient(ByVal emailAddress As String, ByVal theScheduler As scheduler) As NotificationAddress |
| Dim recipients As ADTObjects |
| Dim i As Integer, n As Integer |
| Dim address As NotificationAddress |
| Dim recipient As NotificationIndividual |
| |
| 'GetObjectsOfClassWhere will return all NotificationIndividuals, then we look for the one we want |
| recipients = theScheduler.GetObjectsForClass(ClassIDEnum.CID_NotificationIndividual) |
| |
| For i = 1 To recipients.Count |
| recipient = recipients(i) |
| For n = 1 To recipient.Addresses.Count |
| address = recipient.Addresses(n) |
| If String.Compare(address.RecipientAddress, emailAddress, True) = 0 Then |
| Return recipient |
| End If |
| Next |
| |
| Next |
| |
| 'no existing recipient found; create a new one |
| recipient = theScheduler.CreateObject(ClassIDEnum.CID_NotificationIndividual) |
| recipient.Name = emailAddress |
| |
| address = theScheduler.CreateObject(ClassIDEnum.CID_NotificationAddress) |
| address.AddressType = NotificationAddressTypeEnum.natSMTP |
| address.RecipientAddress = emailAddress |
| recipient.Addresses.Add(address) |
| Return recipient |
| End Function |
| |
| |
| End Module |
| End Namespace |