Event Trigger Example (Scripting)
This scripting example shows how to create a task that sends an email when an event occurs. The task contains an event trigger that specifies an event query that subscribes to an event from the Windows Event Log service. When the event occurs, the task is triggered. The task also contains an email action that sends an email when the task is triggered. A start boundary and an end boundary are also defined for the task.
The following procedure describes how to schedule a task to send an email when an event occurs.
To Schedule a Task to Send an Email Message When an Event Occurs
- Create a TaskService object. This object allows you to create the task in a specified folder.
- Get a task folder and create a task. Use the TaskService.GetFolder method to get the folder where the task is stored and the TaskService.NewTask method to create the TaskDefinition object that represents the task.
- Define information about the task using the TaskDefinition object. Use the TaskDefinition.Settings property to define the settings that determine how the Task Scheduler service performs the task and the TaskDefinition.RegistrationInfo property to define the information that describes the task.
- Create an event trigger using the TaskDefinition.Triggers property. This property provides access to the TriggerCollection object. Use the TriggerCollection.Create method (specifying the type of trigger that you want to create) to create an event trigger. As you create the trigger, set the start boundary and end boundary of the trigger to activate and deactivate the trigger. The start boundary specifies when the task's action will be performed. You must also specify an event query using the EventTrigger.Subscription property. This will subscribe to events from a specific event log, and any new event matching the query will trigger the event to complete an action. For information about how to create an event query, see Event Selection.
- Create an action for the task to execute by using the TaskDefinition.Actions property. This property provides access to the ActionCollection object. Use the ActionCollection.Create method to specify the type of action that you want to create. This example uses an EmailAction object, which represents an action that sends an email message.
- Register the task using the TaskFolder.RegisterTaskDefinition method.
The following VBScript example shows how to schedule a task to send an email when a level 2 event is logged in the System event log.
'---------------------------------------------------------
' This sample schedules a task to send an email message
' when an event occurs.
'---------------------------------------------------------
' A constant that specifies an event trigger.
const TriggerTypeEvent = 0
' A constant that specifies an email action.
const ActionTypeEmail = 6
'********************************************************
' Create the TaskService object.
Set service = CreateObject("Schedule.Service")
call service.Connect()
'********************************************************
' Get a folder to create a task definition in.
Dim rootFolder
Set rootFolder = service.GetFolder("\")
' The taskDefinition variable is the TaskDefinition object.
Dim taskDefinition
' The flags parameter is 0 because it is not supported.
Set taskDefinition = service.NewTask(0)
'********************************************************
' Define information about the task.
' Set the registration info for the task by
' creating the RegistrationInfo object.
Dim regInfo
Set regInfo = taskDefinition.RegistrationInfo
regInfo.Description = "Send an email when an event occurs."
regInfo.Author = "Author Name"
' Set the task setting info for the Task Scheduler by
' creating a TaskSettings object.
Dim settings
Set settings = taskDefinition.Settings
settings.StartWhenAvailable = True
'********************************************************
' Create an event trigger.
Dim triggers
Set triggers = taskDefinition.Triggers
Dim trigger
Set trigger = triggers.Create(TriggerTypeEvent)
' Trigger variables that define when the trigger is active.
Dim startTime, endTime
startTime = "2006-05-02T10:49:02"
endTime = "2006-05-02T10:52:02"
WScript.Echo "startTime :" & startTime
WScript.Echo "endTime :" & endTime
trigger.StartBoundary = startTime
trigger.EndBoundary = endTime
trigger.ExecutionTimeLimit = "PT5M" 'Five minutes
trigger.Id = "EventTriggerId"
' Define the event query. The trigger will start the task
' when an event is received.
trigger.Subscription = "<QueryList> " & _
"<Query Id='1'> " & _
"<Select Path='System'>*[System/Level=2]</Select>" & _
"</Query></QueryList>"
'***********************************************************
' Create the action for the task to execute.
' Add an action to the task. The action sends an email message.
Dim Action
Set Action = taskDefinition.Actions.Create( ActionTypeEmail )
Action.Server = "EMailServerName"
Action.Body = "This is the email message."
Action.From = "[email protected]"
Action.Subject = "This is the email subject."
Action.To = "[email protected]"
Dim EmailAttachments(0)
EmailAttachments(0) = "C:\temp\attachement.txt"
Action.Attachments = EmailAttachments
WScript.Echo "Task definition created."
'***********************************************************
' Register (create) the task.
call rootFolder.RegisterTaskDefinition( _
"Test Event Trigger", taskDefinition, 6, _
, , 3)
WScript.Echo "Task submitted."
Related topics
Send comments about this topic to Microsoft
Build date: 10/16/2012