Message Box Example (Scripting)

This scripting example shows how to create a task that shows a message box when an event occurs. The task contains an event trigger that specifies an event query, which subscribes to an event from the Windows Event Log service. When the event occurs, the task is triggered. The task also contains a message box action that alerts the user with a specific message when the task is triggered. A start boundary time and an end boundary time are also defined for the task. The task is registered using an interactive logon type for the security context of the task, which mean the task will execute if the user who registered the task is logged on and an event is received.

The following procedure describes how to schedule a task to show a message box when an event occurs.

Aa381916.wedge(en-us,VS.85).gifTo schedule a task to show a message box when an event occurs

  1. Create a TaskService object. This object allows you to create the task in a specified folder.

  2. 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. The root task folder is specified with a backslash (\). An example of a task folder path, under the root task folder, is \MyTaskFolder.

  3. 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.

  4. 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 task to complete an action. For information about how to create an event query, see Event Selection.

    You can also specify named XPath queries in the EventTrigger.ValueQueries property. Each specified XPath query is applied to the last matching event XML that is returned from the subscription query specified in the EventTrigger.Subscription property. The name of the query can be used as a variable in the message of a ShowMessageAction action.

  5. 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 a ShowMessageAction object, which represents an action that shows a message box. The message references parameterized values from the EventTrigger.ValueQueries property.

  6. Register the task using the TaskFolder.RegisterTaskDefinition method.

The following VBScript code example shows how to schedule a task to show a message box when a level 4 event is logged in the System event log.

'------------------------------------------------
' This sample schedules a task to show 
' a message box when an event occurs.
'------------------------------------------------


' A constant that specifies an event trigger.
const TriggerTypeEvent = 0
' A constant that specifies a message box action.
const ActionTypeShowMessage = 7   

'********************************************************
' 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 = "Show a message box 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-02-28T10:49:02"
endTime = "2007-05-02T10:52:02"

WScript.Echo "startTime :" & startTime
WScript.Echo "endTime :" & endTime

trigger.StartBoundary = startTime
trigger.EndBoundary = endTime
trigger.ExecutionTimeLimit = "PT50M"  'Fifty 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=4]</Select>" & _
    "</Query></QueryList>"   

Dim valueQueries
Set valueQueries = trigger.ValueQueries 
call valueQueries.Create( _
    "eventID", "Event/System/EventRecordID") 

'***********************************************************
' Create the action for the task to execute.

' Add an action to the task. The action shows a message box.
Dim Action
Set Action = taskDefinition.Actions.Create(ActionTypeShowMessage)
Action.MessageBody = "This is the event ID: $(eventID)"
Action.Title = "Message Box Title"

WScript.Echo "Task definition created with an event trigger and message box action."

'***********************************************************
' Register (create) the task.

call rootFolder.RegisterTaskDefinition( _
    "Test Event Trigger", taskDefinition, 6, _
    , , 3)

WScript.Echo "Task submitted."
WScript.Echo "A message box will appear when " _
   & "a level 4 event is logged in the System event log."

Using the Task Scheduler

 

 

Send comments about this topic to Microsoft

Build date: 10/16/2012