启动触发器示例 (XML)

此示例中的 XML 定义一个任务,该任务在系统启动时启动记事本。

若要注册 XML 中定义的任务,可以使用 ITaskFolder::RegisterTask 函数 (TaskFolder.RegisterTask 编写脚本) 或Schtasks.exe命令行工具。 如果使用位于 C:\Windows\System32 目录) 中的 Schtasks.exe 工具 (,则可以使用以下命令注册任务:包含任务定义>/tn 任务名称的 XML 文件的 schtasks /create</XML 路径>。<

定义任务以在系统启动时启动记事本

下面的 XML 示例演示如何定义具有单个执行操作的任务 (启动记事本) 、启动系统时启动任务的单个启动触发器,以及影响任务计划程序处理任务方式的其他几个任务设置。

<?xml version="1.0" ?>
<!--
This sample schedules a task to start notepad.exe when
the system is booted.
-->
<Task xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
    <RegistrationInfo>
        <Date>2005-10-11T13:21:17-08:00</Date>
        <Author>AuthorName</Author>
        <Version>1.0.0</Version>
        <Description>Starts Notepad on system boot.</Description>
    </RegistrationInfo>
    <Triggers>
        <BootTrigger>
            <StartBoundary>2005-10-11T13:21:17-08:00</StartBoundary>
            <EndBoundary>2006-01-01T00:00:00-08:00</EndBoundary>
            <Enabled>true</Enabled>
            <ExecutionTimeLimit>PT5M</ExecutionTimeLimit>
        </BootTrigger>
    </Triggers>
    <Principals>
        <Principal>
            <UserId>Administrator</UserId>
            <LogonType>InteractiveToken</LogonType>
        </Principal>
    </Principals>
    <Settings>
        <Enabled>true</Enabled>
        <AllowStartOnDemand>true</AllowStartOnDemand>
        <AllowHardTerminate>true</AllowHardTerminate>
    </Settings>
    <Actions>
        <Exec>
            <Command>notepad.exe</Command>
        </Exec>
    </Actions>
</Task>

TaskScheduler 架构元素

下面是使用此示例时要记住的一些重要元素。

  • RegistrationInfo:包含有关任务的注册信息。
  • 触发器:定义启动任务的触发器。
  • BootTrigger:定义启动触发器。 在这种情况下,仅使用两个子元素:在激活和停用触发器时指定开始和结束边界。
  • 主体:定义任务运行的安全上下文。
  • 设置:定义任务计划程序用于执行任务的任务设置。
  • 操作:定义任务执行的操作。 在本例中,运行记事本。

使用任务计划程序