Compartir a través de


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="https://schemas.microsoft.com/windows/2004/02/mit/task">
    <RegistrationInfo>
        <Date>2006-05-02T13:21:17</Date>
        <Author>AuthorName</Author>
        <Version>1.0.0</Version>
        <Description>Task sends an email when an event occurs.</Description>
    </RegistrationInfo>
    <Triggers>
        <EventTrigger>
            <StartBoundary>2006-05-02T13:20:00</StartBoundary>
            <EndBoundary>2007-05-02T13:20:00</EndBoundary>
            <ExecutionTimeLimit>PT5M</ExecutionTimeLimit>
            <Subscription>
                <QueryList>
                    <Query Id='1'>
                        <Select Path='System'>*[System/Level=2]</Select>
                    </Query>
               </QueryList>
            </Subscription>
        </EventTrigger>
    </Triggers>
    <Principals>
        <Principal>
            <UserId>Administrator</UserId>
            <LogonType>InteractiveToken</LogonType>
        </Principal>
    </Principals>
    <Settings>
        <Enabled>true</Enabled>
        <AllowStartOnDemand>true</AllowStartOnDemand>
        <AllowHardTerminate>true</AllowHardTerminate>
    </Settings>
    <Actions>
        <SendEmail>
            <Server>EMailServerName</Server>
            <Subject>Enter Subject here</Subject>
            <To>RecipientEmailAddress@domain.com</To>
            <From>MyEmailAddress@domain.com</From>
            <Body>This is the email message.</Body>
            <Attachments>
                <File>C:\temp\attachment.txt</File>
            </Attachments>
        </SendEmail>
    </Actions>
</Task>

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.

Using the Task Scheduler

 

 

Send comments about this topic to Microsoft

Build date: 10/16/2012