无法复制有关使用XML文件通过Schtasks.exe计划任务的文档示例

眼罩

概括

我正在遵循有关使用任务计划程序创建以XML定义的任务的Microsoft官方文档的示例我的目标是实现在系统启动时启动记事本的示例。

细节

按照文档中的步骤,我已经创建了一个XML文件,其中包含上面链接的内容,并将其放在中C:\start-notepad.xml要注册任务,我以管理员身份打开PowerShell并运行

PS C:\> schtasks /create /XML start-notepad.xml /tn start-notepad
SUCCESS: The scheduled task "start-notepad" has successfully been created.

PS C:\> schtasks /Query /tn start-notepad

Folder: \
TaskName                                 Next Run Time          Status
======================================== ====================== ===============
start-notepad                            N/A                    Ready

现在,当我重新启动计算机(VirtualBox上的VM)时,我希望记事本应用程序启动,但不会启动。说明文件缺少某些内容,或者我做错了...

start-notepad.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>2026-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>

任务计划程序

该任务已正确添加到任务计划程序中,但是当我单击“运行”时没有任何反应,重新启动时也没有...

任务计划程序

眼罩

我通过更改2件事找到了解决方案

1.使用LogonTrigger旁边的BootTrigger

建议的解决方案在这里:https : //www.sevenforums.com/tutorials/67503-task-create-run-program-startup-log.html

我更换了:

<Triggers>
  <BootTrigger>
    <StartBoundary>2005-10-11T13:21:17-08:00</StartBoundary>
    <EndBoundary>2026-01-01T00:00:00-08:00</EndBoundary>
    <Enabled>true</Enabled>
    <ExecutionTimeLimit>PT5M</ExecutionTimeLimit>
  </BootTrigger>
</Triggers>

和:

<Triggers>
  <BootTrigger>
    <Enabled>true</Enabled>
  </BootTrigger>
  <LogonTrigger>
    <Enabled>true</Enabled>
  </LogonTrigger>
</Triggers>

2.使用正确的 Principal

我发现,除非使用当前登录的用户,否则记事本将永远不会在启动(或登录)时启动。此答案中,我发现了builtin\users无论使用哪个用户登录,都可以使用来启动记事本的建议

我更换了:

<Principal>
  <UserId>Administrator</UserId>
  <LogonType>InteractiveToken</LogonType>
</Principal>

和:

<Principal>
  <GroupId>S-1-5-32-545</GroupId>
  <RunLevel>LeastPrivilege</RunLevel>
</Principal>

您可以GrouId在此处找到有关此内容的更多信息https : //support.microsoft.com/zh-cn/help/243330/well-known-security-identifiers-in-windows-operating-systems

最终的工作XML文件:

<?xml version="1.0" ?>
<Task xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
    <RegistrationInfo>
        <Date>2020-07-29T00:00:00-00:00</Date>
        <Author>AuthorName</Author>
        <Version>1.0.0</Version>
        <Description>Starts Notepad on system boot.</Description>
    </RegistrationInfo>
    <Triggers>
        <BootTrigger>
            <Enabled>true</Enabled>
        </BootTrigger>
        <LogonTrigger>
            <Enabled>true</Enabled>
        </LogonTrigger>
    </Triggers>
    <Principals>
      <Principal>
          <GroupId>S-1-5-32-545</GroupId>
          <RunLevel>LeastPrivilege</RunLevel>
      </Principal>
    </Principals>
    <Settings>
        <Enabled>true</Enabled>
        <AllowStartOnDemand>true</AllowStartOnDemand>
        <AllowHardTerminate>true</AllowHardTerminate>
    </Settings>
    <Actions>
        <Exec>
            <Command>notepad.exe</Command>
        </Exec>
    </Actions>
</Task>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章