Event Trigger Example (XML)

The XML in this example defines a task that sends an email when an event occurs.

To register a task that is defined in XML, you can use either the ITaskFolder::RegisterTask function (TaskFolder.RegisterTask for scripting) or the Schtasks.exe command-line tool. If you use the Schtasks.exe tool (located in the C:\Windows\System32 directory), then you can use the following command to register the task: schtasks /create /XML <path to the XML file containing the task definition> /tn <task name>.

To define a task that sends an email when an event occurs

The following XML example shows how to define a task with an email action, an event trigger that starts the task when a specified event is received, and several other task settings that affect how the task is handled by the Task Scheduler.

<?xml version="1.0" ?>
This sample schedules a task to send an email when an event occurs.
<Task xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
        <Description>Task sends an email when an event occurs.</Description>
                    <Query Id='1'>
                        <Select Path='System'>*[System/Level=2]</Select>
            <Subject>Enter Subject here</Subject>
            <Body>This is the email message.</Body>

TaskScheduler Schema Elements

The following are some important elements to keep in mind when using this example:

  • RegistrationInfo: Contains registration information about the task.
  • Triggers: Defines the trigger that starts the task.
  • EventTrigger: Defines the event trigger. In this case, four child elements are used: the start and end boundaries that specify when the trigger is activated and deactivated, the execution time limit that specifies the maximum amount of time in which the task can be started by the trigger, and the event subscription query. This event subscription queries for level two events from the System event log. For information about how to create an event query, see Event Selection.
  • Principal: Defines the security context that a task runs under.
  • Settings: Defines the task settings that the Task Scheduler uses to perform the task.
  • Actions: Defines the actions that the task performs. The task example above contains an email action. For more information, see sendEmailType Complex Type.

Related topics

Using the Task Scheduler



Send comments about this topic to Microsoft

Build date: 10/16/2012

Community Additions