使用批处理文件创建计划任务 xml 文件

使用批处理文件创建计划任务 xml 文件

我正在尝试制作一个批处理文件,该文件可以创建一个 xml 文件,然后将该文件导入计划任务,以及一些其他操作。我知道如何导入 xml 并完成我需要做的其他所有事情,但我无法正确创建 xml 文件。

以下是计划任务导出的xml文件内容:

<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.4" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
  <RegistrationInfo>
    <Date>2021-01-11T04:56:22.2785148</Date>
    <Author>Computer_Name\User</Author>
    <URI>\Display Off When Locked (Battery Only)</URI>
  </RegistrationInfo>
  <Triggers>
    <SessionStateChangeTrigger>
      <Enabled>true</Enabled>
      <StateChange>SessionLock</StateChange>
    </SessionStateChangeTrigger>
  </Triggers>
  <Principals>
    <Principal id="Author">
      <GroupId>S-1-5-32-544</GroupId>
      <RunLevel>HighestAvailable</RunLevel>
    </Principal>
  </Principals>
  <Settings>
    <MultipleInstancesPolicy>StopExisting</MultipleInstancesPolicy>
    <DisallowStartIfOnBatteries>false</DisallowStartIfOnBatteries>
    <StopIfGoingOnBatteries>true</StopIfGoingOnBatteries>
    <AllowHardTerminate>true</AllowHardTerminate>
    <StartWhenAvailable>false</StartWhenAvailable>
    <RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
    <IdleSettings>
      <StopOnIdleEnd>true</StopOnIdleEnd>
      <RestartOnIdle>false</RestartOnIdle>
    </IdleSettings>
    <AllowStartOnDemand>true</AllowStartOnDemand>
    <Enabled>true</Enabled>
    <Hidden>false</Hidden>
    <RunOnlyIfIdle>false</RunOnlyIfIdle>
    <DisallowStartOnRemoteAppSession>false</DisallowStartOnRemoteAppSession>
    <UseUnifiedSchedulingEngine>true</UseUnifiedSchedulingEngine>
    <WakeToRun>false</WakeToRun>
    <ExecutionTimeLimit>PT1H</ExecutionTimeLimit>
    <Priority>7</Priority>
  </Settings>
  <Actions Context="Author">
    <Exec>
      <Command>"C:\Portable Apps\MonOff (Display off When Locked)\MonOff (Battery Only).bat"</Command>
    </Exec>
  </Actions>
</Task>

下面是批处理文件的内容,其唯一目的是创建一个相同的 xml 文件:

setlocal enableDelayedExpansion

echo ^<?xml version="1.0" encoding="UTF-16"?^>> task.xml
echo ^<Task version="1.4" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task"^>>> task.xml
echo   ^<RegistrationInfo^>>> task.xml
echo     ^<Date^>2021-01-11T04:56:22.2785148^</Date^>>> task.xml
echo     ^<Author^>Computer_Name\User^</Author^>>> task.xml
echo     ^<URI^>\Display Off When Locked (Battery Only)^</URI^>>> task.xml
echo   ^</RegistrationInfo^>>> task.xml
echo   ^<Triggers^>>> task.xml
echo     ^<SessionStateChangeTrigger^>>> task.xml
echo       ^<Enabled^>true^</Enabled^>>> task.xml
echo       ^<StateChange^>SessionLock^</StateChange^>>> task.xml
echo     ^</SessionStateChangeTrigger^>>> task.xml
echo   ^</Triggers^>>> task.xml
echo   ^<Principals^>>> task.xml
echo     ^<Principal id="Author"^>>> task.xml
echo       ^<GroupId^>S-1-5-32-544^</GroupId^>>> task.xml
echo       ^<RunLevel^>HighestAvailable^</RunLevel^>>> task.xml
echo     ^</Principal^>>> task.xml
echo   ^</Principals^>>> task.xml
echo   ^<Settings^>>> task.xml
echo     ^<MultipleInstancesPolicy^>StopExisting^</MultipleInstancesPolicy^>>> task.xml
echo     ^<DisallowStartIfOnBatteries^>false^</DisallowStartIfOnBatteries^>>> task.xml
echo     ^<StopIfGoingOnBatteries^>true^</StopIfGoingOnBatteries^>>> task.xml
echo     ^<AllowHardTerminate^>true^</AllowHardTerminate^>>> task.xml
echo     ^<StartWhenAvailable^>false^</StartWhenAvailable^>>> task.xml
echo     ^<RunOnlyIfNetworkAvailable^>false^</RunOnlyIfNetworkAvailable^>>> task.xml
echo     ^<IdleSettings^>>> task.xml
echo       ^<StopOnIdleEnd^>true^</StopOnIdleEnd^>>> task.xml
echo       ^<RestartOnIdle^>false^</RestartOnIdle^>>> task.xml
echo     ^</IdleSettings^>>> task.xml
echo     ^<AllowStartOnDemand^>true^</AllowStartOnDemand^>>> task.xml
echo     ^<Enabled^>true^</Enabled^>>> task.xml
echo     ^<Hidden^>false^</Hidden^>>> task.xml
echo     ^<RunOnlyIfIdle^>false^</RunOnlyIfIdle^>>> task.xml
echo     ^<DisallowStartOnRemoteAppSession^>false^</DisallowStartOnRemoteAppSession^>>> task.xml
echo     ^<UseUnifiedSchedulingEngine^>true^</UseUnifiedSchedulingEngine^>>> task.xml
echo     ^<WakeToRun^>false^</WakeToRun^>>> task.xml
echo     ^<ExecutionTimeLimit^>PT1H^</ExecutionTimeLimit^>>> task.xml
echo     ^<Priority^>7^</Priority^>>> task.xml
echo   ^</Settings^>>> task.xml
echo   ^<Actions Context="Author"^>>> task.xml
echo     ^<Exec^>>> task.xml
echo       ^<Command^>"C:\Portable Apps\MonOff (Display off When Locked)\MonOff (Battery Only).bat"^</Command^>>> task.xml
echo     ^</Exec^>>> task.xml
echo   ^</Actions^>>> task.xml
echo ^</Task^>>> task.xml

endlocal & exit /b

当我在文本编辑器中查看它们时,它们是相同的。但是 task.xml 只有 2KB,而原始文件有 4KB,在 WinMerge 中查看它们会显示原始文件的内容,但 task.xml 只会在一行中显示一堆看起来像亚洲字符的字符:

㼼浸敶獲潩㵮ㄢ〮•湥潣楤杮∽呕ⵆ㘱㼢ാ㰊慔歳瘠牥楳湯∽⸱∴砠汭獮∽瑨灴⼺猯档浥獡洮捩潲潳瑦挮浯眯湩潤獷㈯〰⼴㈰洯瑩琯獡≫ാ 㰠敒楧瑳慲楴湯湉潦ാ †㰠慄整㈾㈰ⴱ㄰ㄭ吱㐰㔺㨶㈲㈮㠷ㄵ㠴⼼慄整ാ †㰠畁桴牯䔾协卜整敶⼼畁桴牯ാ †㰠剕㹉䑜獩汰祡传晦圠敨潌正摥⠠慂瑴牥⁹湏祬㰩唯䥒ാ 㰠刯来獩牴瑡潩䥮普㹯਍†吼楲杧牥㹳਍††匼獥楳湯瑓瑡䍥慨杮呥楲杧牥ാ ††㰠湅扡敬㹤牴敵⼼湅扡敬㹤਍†††匼慴整桃湡敧匾獥楳湯潌正⼼瑓瑡䍥慨杮㹥਍††⼼敓獳潩卮慴整桃湡敧牔杩敧㹲਍†⼼牔杩敧獲ാ 㰠牐湩楣慰獬ാ †㰠牐湩楣慰摩∽畁桴牯㸢਍†††䜼潲灵摉匾ㄭ㔭㌭ⴲ㐵㰴䜯潲灵摉ാ ††㰠畒䱮癥汥䠾杩敨瑳癁楡慬汢㱥刯湵敌敶㹬਍††⼼牐湩楣慰㹬਍†⼼牐湩楣慰獬ാ 㰠敓瑴湩獧ാ †㰠畍瑬灩敬湉瑳湡散偳汯捩㹹瑓灯硅獩楴杮⼼畍瑬灩敬湉瑳湡散偳汯捩㹹਍††䐼獩污潬卷慴瑲晉湏慂瑴牥敩㹳慦獬㱥䐯獩污潬卷慴瑲晉湏慂瑴牥敩㹳਍††匼潴䥰䝦楯杮湏慂瑴牥敩㹳牴敵⼼瑓灯晉潇湩佧䉮瑡整楲獥ാ †㰠汁潬䡷牡呤牥業慮整琾畲㱥䄯汬睯慈摲敔浲湩瑡㹥਍††匼慴瑲桗湥癁楡慬汢㹥慦獬㱥匯慴瑲桗湥癁楡慬汢㹥਍††刼湵湏祬晉敎睴牯䅫慶汩扡敬显污敳⼼畒佮汮䥹书瑥潷歲癁楡慬汢㹥਍††䤼汤卥瑥楴杮㹳਍†††匼潴佰䥮汤䕥摮琾畲㱥匯潴佰䥮汤䕥摮ാ ††㰠敒瑳牡佴䥮汤㹥慦獬㱥刯獥慴瑲湏摉敬ാ †㰠䤯汤卥瑥楴杮㹳਍††䄼汬睯瑓牡佴䑮浥湡㹤牴敵⼼汁潬卷慴瑲湏敄慭摮ാ †㰠湅扡敬㹤牴敵⼼湅扡敬㹤਍††䠼摩敤㹮慦獬㱥䠯摩敤㹮਍††刼湵湏祬晉摉敬显污敳⼼畒佮汮䥹䥦汤㹥਍††䐼獩污潬卷慴瑲湏敒潭整灁印獥楳湯显污敳⼼楄慳汬睯瑓牡佴剮浥瑯䅥灰敓獳潩㹮਍††唼敳湕晩敩卤档摥汵湩䕧杮湩㹥牴敵⼼獕啥楮楦摥捓敨畤楬杮湅楧敮ാ †㰠慗敫潔畒㹮慦獬㱥圯歡呥副湵ാ †㰠硅捥瑵潩呮浩䱥浩瑩倾ㅔ㱈䔯數畣楴湯楔敭楌業㹴਍††值楲牯瑩㹹㰷倯楲牯瑩㹹਍†⼼敓瑴湩獧ാ 㰠捁楴湯⁳潃瑮硥㵴䄢瑵潨≲ാ †㰠硅捥ാ ††㰠潃浭湡㹤䌢尺潐瑲扡敬䄠灰屳潍佮晦⠠楄灳慬⁹景⁦桗湥䰠捯敫⥤䵜湯晏⁦䈨瑡整祲传汮⥹戮瑡㰢䌯浯慭摮ാ †㰠䔯數㹣਍†⼼捁楴湯㹳਍⼼慔歳ാ

当然,这个新创建的 xml 不会导入到任务计划程序中,从而出现以下错误:

任务格式无效。报告了以下错误:(1,2)::ERROR: 文档语法不正确

此外,HxD 显示两个文件的内容有很大差异:

原来的:

ÿþ<�?�x�m�l� �v�e�r�s�i�o�n�=�"�1�.�0�"� �e�n�c�o�d�i�n�g�=�"�U�T�F�-�1�6�"�?�>�
�<�T�a�s�k� �v�e�r�s�i�o�n�=�"�1�.�4�"� �x�m�l�n�s�=�"�h�t�t�p�:�/�/�s�c�h�e�m�a�s�.�m�i�c�r�o�s�o�f�t�.�c�o�m�/�w�i�n�d�o�w�s�/�2�0�0�4�/�0�2�/�m�i�t�/�t�a�s�k�"�>�
� � �<�R�e�g�i�s�t�r�a�t�i�o�n�I�n�f�o�>�
� � � � �<�D�a�t�e�>�2�0�2�1�-�0�1�-�1�1�T�0�4�:�5�6�:�2�2�.�2�7�8�5�1�4�8�<�/�D�a�t�e�>�
� � � � �<�A�u�t�h�o�r�>�C�o�m�p�u�t�e�r�_�N�a�m�e�\�U�s�e�r�<�/�A�u�t�h�o�r�>�
� � � � �<�U�R�I�>�\�D�i�s�p�l�a�y� �O�f�f� �W�h�e�n� �L�o�c�k�e�d� �(�B�a�t�t�e�r�y� �O�n�l�y�)�<�/�U�R�I�>�
� � �<�/�R�e�g�i�s�t�r�a�t�i�o�n�I�n�f�o�>�
� � �<�T�r�i�g�g�e�r�s�>�
� � � � �<�S�e�s�s�i�o�n�S�t�a�t�e�C�h�a�n�g�e�T�r�i�g�g�e�r�>�
� � � � � � �<�E�n�a�b�l�e�d�>�t�r�u�e�<�/�E�n�a�b�l�e�d�>�
� � � � � � �<�S�t�a�t�e�C�h�a�n�g�e�>�S�e�s�s�i�o�n�L�o�c�k�<�/�S�t�a�t�e�C�h�a�n�g�e�>�
� � � � �<�/�S�e�s�s�i�o�n�S�t�a�t�e�C�h�a�n�g�e�T�r�i�g�g�e�r�>�
� � �<�/�T�r�i�g�g�e�r�s�>�
� � �<�P�r�i�n�c�i�p�a�l�s�>�
� � � � �<�P�r�i�n�c�i�p�a�l� �i�d�=�"�A�u�t�h�o�r�"�>�
� � � � � � �<�G�r�o�u�p�I�d�>�S�-�1�-�5�-�3�2�-�5�4�4�<�/�G�r�o�u�p�I�d�>�
� � � � � � �<�R�u�n�L�e�v�e�l�>�H�i�g�h�e�s�t�A�v�a�i�l�a�b�l�e�<�/�R�u�n�L�e�v�e�l�>�
� � � � �<�/�P�r�i�n�c�i�p�a�l�>�
� � �<�/�P�r�i�n�c�i�p�a�l�s�>�
� � �<�S�e�t�t�i�n�g�s�>�
� � � � �<�M�u�l�t�i�p�l�e�I�n�s�t�a�n�c�e�s�P�o�l�i�c�y�>�S�t�o�p�E�x�i�s�t�i�n�g�<�/�M�u�l�t�i�p�l�e�I�n�s�t�a�n�c�e�s�P�o�l�i�c�y�>�
� � � � �<�D�i�s�a�l�l�o�w�S�t�a�r�t�I�f�O�n�B�a�t�t�e�r�i�e�s�>�f�a�l�s�e�<�/�D�i�s�a�l�l�o�w�S�t�a�r�t�I�f�O�n�B�a�t�t�e�r�i�e�s�>�
� � � � �<�S�t�o�p�I�f�G�o�i�n�g�O�n�B�a�t�t�e�r�i�e�s�>�t�r�u�e�<�/�S�t�o�p�I�f�G�o�i�n�g�O�n�B�a�t�t�e�r�i�e�s�>�
� � � � �<�A�l�l�o�w�H�a�r�d�T�e�r�m�i�n�a�t�e�>�t�r�u�e�<�/�A�l�l�o�w�H�a�r�d�T�e�r�m�i�n�a�t�e�>�
� � � � �<�S�t�a�r�t�W�h�e�n�A�v�a�i�l�a�b�l�e�>�f�a�l�s�e�<�/�S�t�a�r�t�W�h�e�n�A�v�a�i�l�a�b�l�e�>�
� � � � �<�R�u�n�O�n�l�y�I�f�N�e�t�w�o�r�k�A�v�a�i�l�a�b�l�e�>�f�a�l�s�e�<�/�R�u�n�O�n�l�y�I�f�N�e�t�w�o�r�k�A�v�a�i�l�a�b�l�e�>�
� � � � �<�I�d�l�e�S�e�t�t�i�n�g�s�>�
� � � � � � �<�S�t�o�p�O�n�I�d�l�e�E�n�d�>�t�r�u�e�<�/�S�t�o�p�O�n�I�d�l�e�E�n�d�>�
� � � � � � �<�R�e�s�t�a�r�t�O�n�I�d�l�e�>�f�a�l�s�e�<�/�R�e�s�t�a�r�t�O�n�I�d�l�e�>�
� � � � �<�/�I�d�l�e�S�e�t�t�i�n�g�s�>�
� � � � �<�A�l�l�o�w�S�t�a�r�t�O�n�D�e�m�a�n�d�>�t�r�u�e�<�/�A�l�l�o�w�S�t�a�r�t�O�n�D�e�m�a�n�d�>�
� � � � �<�E�n�a�b�l�e�d�>�t�r�u�e�<�/�E�n�a�b�l�e�d�>�
� � � � �<�H�i�d�d�e�n�>�f�a�l�s�e�<�/�H�i�d�d�e�n�>�
� � � � �<�R�u�n�O�n�l�y�I�f�I�d�l�e�>�f�a�l�s�e�<�/�R�u�n�O�n�l�y�I�f�I�d�l�e�>�
� � � � �<�D�i�s�a�l�l�o�w�S�t�a�r�t�O�n�R�e�m�o�t�e�A�p�p�S�e�s�s�i�o�n�>�f�a�l�s�e�<�/�D�i�s�a�l�l�o�w�S�t�a�r�t�O�n�R�e�m�o�t�e�A�p�p�S�e�s�s�i�o�n�>�
� � � � �<�U�s�e�U�n�i�f�i�e�d�S�c�h�e�d�u�l�i�n�g�E�n�g�i�n�e�>�t�r�u�e�<�/�U�s�e�U�n�i�f�i�e�d�S�c�h�e�d�u�l�i�n�g�E�n�g�i�n�e�>�
� � � � �<�W�a�k�e�T�o�R�u�n�>�f�a�l�s�e�<�/�W�a�k�e�T�o�R�u�n�>�
� � � � �<�E�x�e�c�u�t�i�o�n�T�i�m�e�L�i�m�i�t�>�P�T�1�H�<�/�E�x�e�c�u�t�i�o�n�T�i�m�e�L�i�m�i�t�>�
� � � � �<�P�r�i�o�r�i�t�y�>�7�<�/�P�r�i�o�r�i�t�y�>�
� � �<�/�S�e�t�t�i�n�g�s�>�
� � �<�A�c�t�i�o�n�s� �C�o�n�t�e�x�t�=�"�A�u�t�h�o�r�"�>�
� � � � �<�E�x�e�c�>�
� � � � � � �<�C�o�m�m�a�n�d�>�"�C�:�\�P�o�r�t�a�b�l�e� �A�p�p�s�\�M�o�n�O�f�f� �(�D�i�s�p�l�a�y� �o�f�f� �W�h�e�n� �L�o�c�k�e�d�)�\�M�o�n�O�f�f� �(�B�a�t�t�e�r�y� �O�n�l�y�)�.�b�a�t�"�<�/�C�o�m�m�a�n�d�>�
� � � � �<�/�E�x�e�c�>�
� � �<�/�A�c�t�i�o�n�s�>�
�<�/�T�a�s�k�>�

清理版本(删除了�):

ÿþ<?xml version="1.0" encoding="UTF-16"?>

<Task version="1.4" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">

  <RegistrationInfo>

    <Date>2021-01-11T04:56:22.2785148</Date>

    <Author>Computer_Name\User</Author>

    <URI>\Display Off When Locked (Battery Only)</URI>

  </RegistrationInfo>

  <Triggers>

    <SessionStateChangeTrigger>

      <Enabled>true</Enabled>

      <StateChange>SessionLock</StateChange>

    </SessionStateChangeTrigger>

  </Triggers>

  <Principals>

    <Principal id="Author">

      <GroupId>S-1-5-32-544</GroupId>

      <RunLevel>HighestAvailable</RunLevel>

    </Principal>

  </Principals>

  <Settings>

    <MultipleInstancesPolicy>StopExisting</MultipleInstancesPolicy>

    <DisallowStartIfOnBatteries>false</DisallowStartIfOnBatteries>

    <StopIfGoingOnBatteries>true</StopIfGoingOnBatteries>

    <AllowHardTerminate>true</AllowHardTerminate>

    <StartWhenAvailable>false</StartWhenAvailable>

    <RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>

    <IdleSettings>

      <StopOnIdleEnd>true</StopOnIdleEnd>

      <RestartOnIdle>false</RestartOnIdle>

    </IdleSettings>

    <AllowStartOnDemand>true</AllowStartOnDemand>

    <Enabled>true</Enabled>

    <Hidden>false</Hidden>

    <RunOnlyIfIdle>false</RunOnlyIfIdle>

    <DisallowStartOnRemoteAppSession>false</DisallowStartOnRemoteAppSession>

    <UseUnifiedSchedulingEngine>true</UseUnifiedSchedulingEngine>

    <WakeToRun>false</WakeToRun>

    <ExecutionTimeLimit>PT1H</ExecutionTimeLimit>

    <Priority>7</Priority>

  </Settings>

  <Actions Context="Author">

    <Exec>

      <Command>"C:\Portable Apps\MonOff (Display off When Locked)\MonOff (Battery Only).bat"</Command>

    </Exec>

  </Actions>

</Task>

任务.xml:

ÿþ<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.4" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
  <RegistrationInfo>
    <Date>2021-01-11T04:56:22.2785148</Date>
    <Author>Computer_Name\User</Author>
    <URI>\Display Off When Locked (Battery Only)</URI>
  </RegistrationInfo>
  <Triggers>
    <SessionStateChangeTrigger>
      <Enabled>true</Enabled>
      <StateChange>SessionLock</StateChange>
    </SessionStateChangeTrigger>
  </Triggers>
  <Principals>
    <Principal id="Author">
      <GroupId>S-1-5-32-544</GroupId>
      <RunLevel>HighestAvailable</RunLevel>
    </Principal>
  </Principals>
  <Settings>
    <MultipleInstancesPolicy>StopExisting</MultipleInstancesPolicy>
    <DisallowStartIfOnBatteries>false</DisallowStartIfOnBatteries>
    <StopIfGoingOnBatteries>true</StopIfGoingOnBatteries>
    <AllowHardTerminate>true</AllowHardTerminate>
    <StartWhenAvailable>false</StartWhenAvailable>
    <RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
    <IdleSettings>
      <StopOnIdleEnd>true</StopOnIdleEnd>
      <RestartOnIdle>false</RestartOnIdle>
    </IdleSettings>
    <AllowStartOnDemand>true</AllowStartOnDemand>
    <Enabled>true</Enabled>
    <Hidden>false</Hidden>
    <RunOnlyIfIdle>false</RunOnlyIfIdle>
    <DisallowStartOnRemoteAppSession>false</DisallowStartOnRemoteAppSession>
    <UseUnifiedSchedulingEngine>true</UseUnifiedSchedulingEngine>
    <WakeToRun>false</WakeToRun>
    <ExecutionTimeLimit>PT1H</ExecutionTimeLimit>
    <Priority>7</Priority>
  </Settings>
  <Actions Context="Author">
    <Exec>
      <Command>"C:\Portable Apps\MonOff (Display off When Locked)\MonOff (Battery Only).bat"</Command>
    </Exec>
  </Actions>
</Task>

如您所见,从计划任务导出的 xml 中有很多额外内容(Unicode 字符?)。此时我能想到的唯一办法就是尝试将 HxD 中的内容复制/粘贴到批处理文件中,并将所有这些额外字符回显到 xml 文件中,假设它会这样做,这就是我接下来要尝试的。我需要这样做才能根据计算机之间可能不同的某些变量自动创建计划任务。任何帮助都将不胜感激。谢谢!

答案1

TL;DR - 如果您只想跳到解决方案并跳过上下文和其他信息以获得在类似情况下进行设置的额外帮助,即您有特定需求并且只想知道如何去做,我已经将所需的信息括在 # 中(++###+ 表示部分开始,-###- 表示结束)。感谢用户 382099/it-wasnt-me 为解决这个问题提供的巨大帮助。

经过相当长的时间和反复试验,我开发了两个解决方案。第一个是问题的实际答案,这要归功于用户 382099/it-wasnt-me,他花了很多时间耐心地与我一起开发它。我帮助测试和故障排除,并清理代码以删除最终对完成任务不必要的部分,但他们创建了所有代码。不幸的是,他们删除了他们的答案,认为我可以根据我们的最终结果提供一个更清晰的答案,所以我不能选择他们的答案作为解决方案,但我一定要给予他们荣誉,并感谢他们分享他们的时间和知识来提出这个解决方案。

首先,我要解释一下我在做什么以及为什么这么做。我把所有软件都放在 D:\Programs 中,虽然已安装的软件会转到 C:\Program Files 和 C:\Program Files (x86),但便携式软件、调整等不会。因此,我创建了“安装程序”批处理文件,用于在其他系统上轻松设置这些文件,但需要考虑可能的差异,在这种情况下,不同的计算机和用户名(用于在计划任务中匹配正在设置的系统上的计算机和用户)以及可能不同的便携式应用程序“安装”目录(以告诉计划任务运行文件的位置)。然后,我在 D:\Programs 文件夹中有一个名为 !Portable_Apps_Directory! 的文本文件,文件的内容是便携式应用程序目录的路径(在我的情况下,我使用的是 C:\Portable Apps),因此,要更改不同设置的位置,只需在运行所有“安装程序”之前编辑该文件即可。

为了回答这个问题,我将分享我创建的各种脚本,用于设置计划任务以在锁定计算机时关闭显示器,因为它们既包含解决方案 382099/it-wasnt-me 也包含我想到的解决方案,并展示了我如何执行其他“安装”方面,以防其他人发现它对做类似的事情有用,并且它也可能帮助其他人设置他们的计算机以自动关闭显示器(网上有相当简单的说明来做到这一点,但通过自动化可能会让它更容易一些)。为了做到这一点,您必须首先下载 nircmd.exe 并将其放在与这些批处理文件以及前面提到的 !Portable_Apps_Directory!.txt 文件相同的驱动器上。此任务有两个版本,一个版本在计算机锁定时关闭显示屏,无论计算机使用的是交流电源还是电池(以最大程度地节省电量),另一个版本仅在使用电池时关闭显示屏(以防您希望能够锁定计算机但仍能看到时钟,例如在办公室使用)。因此,有两个“安装程序”,每个安装程序都有自己的辅助批处理文件,它们调用这些批处理文件来创建适用的任务 xml 文件,而仅使用电池的安装程序还会复制额外的批处理文件,该文件是确定是使用交流电源还是电池电源所必需的,并且仅在使用电池时关闭显示屏。

+##################################################################################+

首先,在深入研究更为复杂的实际解决方案之前,另一种“解决方案”只是实现此目的的另一种方法,实际上要简单得多,希望完成类似任务的人可能更喜欢使用它。您可以使用已创建的任务并使用批处理文件来编辑和导入它,而不是使用批处理文件来创建和导入任务。因此,只需将其从已设置的计算机中导出,并将其放在具有批处理文件的目录中,该批处理文件执行以下操作(请记住,您需要以管理员身份运行它,我还没有测试它以确保任务创建得足够快以使 /change 行起作用):

schtasks.exe /create /xml "path\filename.xml" /tn "Task Name"
schtasks.exe /change "Task Name" /tr "'%portable_apps_dir%\file_to_run' arguments"

另一个选择是使用 schtasks.exe /create 从头开始​​构建任务,例如:

schtasks /Create /RU Administrator /RL highest /TN "Windows Defender Update Test" /TR "'C:\Program Files\Windows Defender\MpCmdRun.exe' -SignatureUpdate" /SC ONEVENT /EC Microsoft-Windows-WindowsUpdateClient/Operational /MO *[System/EventID=41]

创建任务以在下载 Windows 更新时运行 Windows Defender 更新。这是必要的,因为 MS 凭借其无穷智慧决定,如果您将 Windows 更新设置为下载但不安装(他们几乎强迫您这样做,因为一旦安装更新就会强制您的系统重新启动,这是绝对不行的),它甚至不会自动更新 WD 的定义,没有单独的选项来更新它们(就我而言,这只是不使用他们的糟糕产品的另一个原因,因为 MS 显然认为尝试使用此方法作为另一种强迫用户接受自动更新的方法是合适的)。大多数人每天都会运行它,但我让它在下载任何更新时运行,这样它就会立即发生(理论上如此,但我并不确信是这样)。无论如何,它展示了另一种通过批处理创建任务的方法,这种方法对许多人来说可能很明显,但我一开始并没有想到,尽管它更有限,因为您无法设置许多选项。

-#################################################################################-

这是另一个解决方案:

由用户 382099/it-wasnt-me 编写的最终代码(不推荐,因为有多余的代码最终不需要,但为了获得荣誉并保留他们的工作,以防有人最终遇到清理代码的问题,因此可能会发现这很有用):

@echo off

cd /d "%~dp0" && chcp 65001 >nul && set "_Portable_Apps=%~dp0"
for /f tokens^=* %%i in ('cmd /u /c ^<con: findstr /e ">" "%~dpnx0"
')do <nul (call set /p "'=%%~i"&echo\)>>"%~dpnx0TempXML.tmp"
powershell -nop -c "gc '%~dpnx0TempXML.tmp'|Out-File -FilePath '%~dp0Display Off When Locked (Battery Only).xml' -Encoding UNICODE"
echo="%_Portable_Apps\%">"%temp%\_Portable_Apps_var.txt"  & 2>nul del /q /f "%~dpnx0TempXML.tmp" & goto :eof

<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.4" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
  <RegistrationInfo>
    <Date>2021-01-11T04:56:22.2785148</Date>
    <Author>%ComputerName%\%Username%</Author>
    <URI>\Display Off When Locked (Battery Only)</URI>
  </RegistrationInfo>
  <Triggers>
    <SessionStateChangeTrigger>
      <Enabled>true</Enabled>
      <StateChange>SessionLock</StateChange>
    </SessionStateChangeTrigger>
  </Triggers>
  <Principals>
    <Principal id="Author">
      <GroupId>S-1-5-32-544</GroupId>
      <RunLevel>HighestAvailable</RunLevel>
    </Principal>
  </Principals>
  <Settings>
    <MultipleInstancesPolicy>StopExisting</MultipleInstancesPolicy>
    <DisallowStartIfOnBatteries>false</DisallowStartIfOnBatteries>
    <StopIfGoingOnBatteries>true</StopIfGoingOnBatteries>
    <AllowHardTerminate>true</AllowHardTerminate>
    <StartWhenAvailable>false</StartWhenAvailable>
    <RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
    <IdleSettings>
      <StopOnIdleEnd>true</StopOnIdleEnd>
      <RestartOnIdle>false</RestartOnIdle>
    </IdleSettings>
    <AllowStartOnDemand>true</AllowStartOnDemand>
    <Enabled>true</Enabled>
    <Hidden>false</Hidden>
    <RunOnlyIfIdle>false</RunOnlyIfIdle>
    <DisallowStartOnRemoteAppSession>false</DisallowStartOnRemoteAppSession>
    <UseUnifiedSchedulingEngine>true</UseUnifiedSchedulingEngine>
    <WakeToRun>false</WakeToRun>
    <ExecutionTimeLimit>PT1H</ExecutionTimeLimit>
    <Priority>7</Priority>
  </Settings>
  <Actions Context="Author">
    <Exec>
      <Command>"%_Portable_Apps%MonOff (Battery Only).bat"</Command>
    </Exec>
  </Actions>
</Task>
rem :: this line is required... keep here.... 

+####################################################################################+

最后清理的代码,对以“chcp 65001 >nul”行开头的问题的实际回答(始终 - 放入“Make Task - Display Off When Locked (Always).bat”):

@echo off
setlocal enableDelayedExpansion

REM set batch file path to variable since %~dp0 is bugged and changes to %cd% when changing directories if %~dp0 is quoted and therefore can't be relied on
set "dp0=%~dp0"
cd /d "%dp0%"
for %%I in (.) do set "dest_dir=%%~nxI"

:loop_portable_apps_dir
dir "^!Portable_Apps_Directory^!.txt"
if %errorlevel% equ 1 (
   REM Check if in root directory to keep from trying to move up forever
   if "%cd:~3,1%"=="" (
      cls
      echo ERROR: !Portable_Apps_Directory!.txt NOT FOUND, MAKE SURE FILE WITH THIS NAME EXISTS IN A PARENT DIRECTORY OF THIS DIRECTORY ( "%dp0%" ^) AND THAT IT INCLUDES THE PATH TO THE DESIRED PORTABLE APPS DIRECTORY.
      pause
      endlocal & exit /b
   ) else (
      cd..
      goto loop_portable_apps_dir
   )
) else (
   SET /p portable_apps_dir=<"^!Portable_Apps_Directory^!.txt"
)

for /f "tokens=*" %%a in ('where /r "%portable_apps_dir%" nircmd.exe') do set nircmd=%%a

if EXIST "%dp0%\Display Off When Locked (Always) Scheduled Task.xml" del "%dp0%\Display Off When Locked (Always) Scheduled Task.xml"

chcp 65001 >nul
for /f tokens^=* %%i in ('cmd /u /c ^<con: findstr /e ">" "%dp0%\%~nx0"') do <nul (call set /p "'=%%~i"&echo\)>>"%dp0%\Display Off When Locked (Always) Scheduled Task.xml"
goto :eof

<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.4" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
  <RegistrationInfo>
    <Date>2021-01-11T04:56:22.2785148</Date>
    <Author>%ComputerName%\%Username%</Author>
    <URI>\Display Off When Locked (Always)</URI>
  </RegistrationInfo>
  <Triggers>
    <SessionStateChangeTrigger>
      <Enabled>true</Enabled>
      <StateChange>SessionLock</StateChange>
    </SessionStateChangeTrigger>
  </Triggers>
  <Principals>
    <Principal id="Author">
      <GroupId>S-1-5-32-544</GroupId>
      <RunLevel>HighestAvailable</RunLevel>
    </Principal>
  </Principals>
  <Settings>
    <MultipleInstancesPolicy>StopExisting</MultipleInstancesPolicy>
    <DisallowStartIfOnBatteries>false</DisallowStartIfOnBatteries>
    <StopIfGoingOnBatteries>true</StopIfGoingOnBatteries>
    <AllowHardTerminate>true</AllowHardTerminate>
    <StartWhenAvailable>false</StartWhenAvailable>
    <RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
    <IdleSettings>
      <StopOnIdleEnd>true</StopOnIdleEnd>
      <RestartOnIdle>false</RestartOnIdle>
    </IdleSettings>
    <AllowStartOnDemand>true</AllowStartOnDemand>
    <Enabled>true</Enabled>
    <Hidden>false</Hidden>
    <RunOnlyIfIdle>false</RunOnlyIfIdle>
    <DisallowStartOnRemoteAppSession>false</DisallowStartOnRemoteAppSession>
    <UseUnifiedSchedulingEngine>true</UseUnifiedSchedulingEngine>
    <WakeToRun>false</WakeToRun>
    <ExecutionTimeLimit>PT1H</ExecutionTimeLimit>
    <Priority>7</Priority>
  </Settings>
  <Actions Context="Author">
    <Exec>
      <Command>"%nircmd%"</Command>
      <Arguments>monitor off</Arguments>
    </Exec>
  </Actions>
</Task>
REM DON'T DELETE THIS LINE

最后清理的代码,对以“chcp 65001 >nul”行开头的问题的实际回答(仅限电池 - 放入“执行任务 - 锁定时关闭显示(仅限电池).bat”):

@echo off
setlocal enableDelayedExpansion

REM set batch file path to variable since %~dp0 is bugged and changes to %cd% when changing directories if %~dp0 is quoted and therefore can't be relied on
set "dp0=%~dp0"
cd /d "%dp0%"
for %%I in (.) do set "dest_dir=%%~nxI"

:loop_portable_apps_dir
dir "^!Portable_Apps_Directory^!.txt"
if %errorlevel% equ 1 (
   REM Check if in root directory to keep from trying to move up forever
   if "%cd:~3,1%"=="" (
      cls
      echo ERROR: !Portable_Apps_Directory!.txt NOT FOUND, MAKE SURE FILE WITH THIS NAME EXISTS IN A PARENT DIRECTORY OF THIS DIRECTORY ( "%dp0%" ^) AND THAT IT INCLUDES THE PATH TO THE DESIRED PORTABLE APPS DIRECTORY.
      pause
      endlocal & exit /b
   ) else (
      cd..
      goto loop_portable_apps_dir
   )
) else (
   SET /p portable_apps_dir=<"^!Portable_Apps_Directory^!.txt"
)

if EXIST "%dp0%\Display Off When Locked (Battery Only) Scheduled Task.xml" del "%dp0%\Display Off When Locked (Battery Only) Scheduled Task.xml"

chcp 65001 >nul
for /f tokens^=* %%i in ('cmd /u /c ^<con: findstr /e ">" "%dp0%\%~nx0"') do <nul (call set /p "'=%%~i"&echo\)>>"%dp0%\Display Off When Locked (Battery Only) Scheduled Task.xml"
goto :eof

<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.4" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
  <RegistrationInfo>
    <Date>2021-01-11T04:56:22.2785148</Date>
    <Author>%ComputerName%\%Username%</Author>
    <URI>\Display Off When Locked (Battery Only)</URI>
  </RegistrationInfo>
  <Triggers>
    <SessionStateChangeTrigger>
      <Enabled>true</Enabled>
      <StateChange>SessionLock</StateChange>
    </SessionStateChangeTrigger>
  </Triggers>
  <Principals>
    <Principal id="Author">
      <GroupId>S-1-5-32-544</GroupId>
      <RunLevel>HighestAvailable</RunLevel>
    </Principal>
  </Principals>
  <Settings>
    <MultipleInstancesPolicy>StopExisting</MultipleInstancesPolicy>
    <DisallowStartIfOnBatteries>false</DisallowStartIfOnBatteries>
    <StopIfGoingOnBatteries>true</StopIfGoingOnBatteries>
    <AllowHardTerminate>true</AllowHardTerminate>
    <StartWhenAvailable>false</StartWhenAvailable>
    <RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
    <IdleSettings>
      <StopOnIdleEnd>true</StopOnIdleEnd>
      <RestartOnIdle>false</RestartOnIdle>
    </IdleSettings>
    <AllowStartOnDemand>true</AllowStartOnDemand>
    <Enabled>true</Enabled>
    <Hidden>false</Hidden>
    <RunOnlyIfIdle>false</RunOnlyIfIdle>
    <DisallowStartOnRemoteAppSession>false</DisallowStartOnRemoteAppSession>
    <UseUnifiedSchedulingEngine>true</UseUnifiedSchedulingEngine>
    <WakeToRun>false</WakeToRun>
    <ExecutionTimeLimit>PT1H</ExecutionTimeLimit>
    <Priority>7</Priority>
  </Settings>
  <Actions Context="Author">
    <Exec>
      <Command>"%portable_apps_dir%\%dest_dir%\MonOff (Battery Only).bat"</Command>
    </Exec>
  </Actions>
</Task>
REM DON'T DELETE THIS LINE

-#################################################################################-

以下是上述整个设置所需的附加文件的代码:

“!Setup(始终关闭显示 - 以管理员身份运行).bat”:

@echo off
setlocal enableDelayedExpansion

REM set batch file path to variable since %~dp0 is bugged and changes to %cd% when changing directories if %~dp0 is quoted and therefore can't be relied on
set "dp0=%~dp0"
cd /d "%dp0%"

net session 1>nul 2>nul
if %errorlevel% neq 0 cls & echo. & echo ERROR: RUN SCRIPT AS ADMINISTRATOR & echo. & pause & endlocal & exit /b

:loop_nircmd_dir
where /r . nircmd.exe 1>nul 2>nul
if %errorlevel% equ 1 (
   REM Check if in root directory to keep from trying to move up forever
   if "%cd:~3,1%"=="" (
      cls
      echo ERROR: nircmd.exe NOT FOUND, MAKE SURE FILE WITH THIS NAME EXISTS ON THIS DRIVE.
      pause
      endlocal & exit /b
   ) else (
      cd..
      goto loop_nircmd_dir
   )
) else (
   for /f "tokens=*" %%a in ('where /r .. nircmd.exe') do set nircmd=%%a
   for %%A in ("!nircmd!") do set nircmd_path=%%~dpA
   cd "!nircmd_path!"
   for %%I in (.) do set "nircmd_dir_name=%%~nxI"
   set "nircmd_path=!cd!"
   cd /d "%dp0%"
)

:loop_portable_apps_dir
dir "^!Portable_Apps_Directory^!.txt"
if %errorlevel% equ 1 (
   REM Check if in root directory to keep from trying to move up forever
   if "%cd:~3,1%"=="" (
      cls
      echo ERROR: !Portable_Apps_Directory!.txt NOT FOUND, MAKE SURE FILE WITH THIS NAME EXISTS IN A PARENT DIRECTORY OF THIS DIRECTORY ( "%dp0%" ^) AND THAT IT INCLUDES THE PATH TO THE DESIRED PORTABLE APPS DIRECTORY.
      pause
      endlocal & exit /b
   ) else (
      cd..
      goto loop_portable_apps_dir
   )
) else (
   SET /p portable_apps_dir=<"^!Portable_Apps_Directory^!.txt"
)

mkdir "%portable_apps_dir%\%nircmd_dir_name%"
robocopy /e "%nircmd_path%" "%portable_apps_dir%\%nircmd_dir_name%"
if %errorlevel% gtr 7 echo. & echo ERROR COPYING WITH ROBOCOPY & echo. & pause & endlocal & exit /b

call "%dp0%\Make Task - Display Off When Locked (Always).bat"
schtasks.exe /Delete /f /tn "Display Off When Locked (Battery Only)" 1>nul 2>nul
schtasks.exe /Delete /f /tn "Display Off When Locked (Always)" 1>nul 2>nul
schtasks.exe /Create /XML "%dp0%\Display Off When Locked (Always) Scheduled Task.xml" /tn "Display Off When Locked (Always)"
if %errorlevel% equ 1 (
   if NOT EXIST "%dp0%\Display Off When Locked (Always) Scheduled Task.xml" (
      cls & echo UNABLE TO CREATE TASK - "%dp0%\Display Off When Locked (Always) Scheduled Task.xml" SHOULD BE CREATED BY THIS SCRIPT BUT DOES NOT EXIST. & echo. & pause
   ) else (
      cls & echo UNABLE TO CREATE TASK - ACCESS DENIED OR PROBLEM WITH "%dp0%\Display Off When Locked (Always) Scheduled Task.xml" RESULTING IN UNSUCCESSFUL IMPORT. RUN SCRIPT AS ADMINISTRATOR, IF STILL GETTING THIS ERROR, CHECK XML FILE AGAINST "%dp0%\-Display Off When Locked (Always) Scheduled Task- TEMPLATE.xml" FOR ERRORS. & echo. & set /p "user_input=Open files in notepad for comparison? [Y/N]"
      if NOT "%user_input%" == "Y" if NOT "%user_input%" == "y" (
         REM Do nothing
      ) else (
         start notepad.exe "%dp0%\Display Off When Locked (Always) Scheduled Task.xml"
         if EXIST "%dp0%\-Display Off When Locked (Always) Scheduled Task - TEMPLATE.xml" start notepad.exe "%dp0%\-Display Off When Locked (Always) Scheduled Task - TEMPLATE.xml"
      )
   )
   cls & echo. & echo SUCCESS^^! & echo. & timeout /t 5
)

endlocal & exit /b

“!Setup(仅在使用电池时关闭显示屏 - 以管理员身份运行).bat”:

@echo off
setlocal enableDelayedExpansion

REM set batch file path to variable since %~dp0 is bugged and changes to %cd% when changing directories if %~dp0 is quoted and therefore can't be relied on
set "dp0=%~dp0"
cd /d "%dp0%"
for %%I in (.) do set "dest_dir=%%~nxI"

net session 1>nul 2>nul
if %errorlevel% neq 0 cls & echo. & echo ERROR: RUN SCRIPT AS ADMINISTRATOR & echo. & pause & endlocal & exit /b

:loop_nircmd_dir
where /r . nircmd.exe 1>nul 2>nul
if %errorlevel% equ 1 (
   REM Check if in root directory to keep from trying to move up forever
   if "%cd:~3,1%"=="" (
      cls
      echo ERROR: nircmd.exe NOT FOUND, MAKE SURE FILE WITH THIS NAME EXISTS ON THIS DRIVE.
      pause
      endlocal & exit /b
   ) else (
      cd..
      goto loop_nircmd_dir
   )
) else (
   for /f "tokens=*" %%a in ('where /r .. nircmd.exe') do set nircmd=%%a
   for %%A in ("!nircmd!") do set nircmd_path=%%~dpA
   cd "!nircmd_path!"
   for %%I in (.) do set "nircmd_dir_name=%%~nxI"
   set "nircmd_path=!cd!"
   cd /d "%dp0%"
)

:loop_portable_apps_dir
dir "^!Portable_Apps_Directory^!.txt"
if %errorlevel% equ 1 (
   REM Check if in root directory to keep from trying to move up forever
   if "%cd:~3,1%"=="" (
      cls
      echo ERROR: !Portable_Apps_Directory!.txt NOT FOUND, MAKE SURE FILE WITH THIS NAME EXISTS IN A PARENT DIRECTORY OF THIS DIRECTORY ( "%dp0%" ^) AND THAT IT INCLUDES THE PATH TO THE DESIRED PORTABLE APPS DIRECTORY.
      pause
      endlocal & exit /b
   ) else (
      cd..
      goto loop_portable_apps_dir
   )
) else (
   SET /p portable_apps_dir=<"^!Portable_Apps_Directory^!.txt"
)

mkdir "%portable_apps_dir%\%dest_dir%"
copy /y "%dp0%\MonOff (Battery Only).bat" "%portable_apps_dir%\%dest_dir%"
mkdir "%portable_apps_dir%\%nircmd_dir_name%"
robocopy /e "%nircmd_path%" "%portable_apps_dir%\%nircmd_dir_name%"
if %errorlevel% gtr 7 echo. & echo ERROR COPYING WITH ROBOCOPY & echo. & pause & endlocal & exit /b

call "%dp0%\Make Task - Display Off When Locked (Battery Only).bat"
schtasks.exe /Delete /f /tn "Display Off When Locked (Always)" 1>nul 2>nul
schtasks.exe /Delete /f /tn "Display Off When Locked (Battery Only)" 1>nul 2>nul
schtasks.exe /Create /XML "%dp0%\Display Off When Locked (Battery Only) Scheduled Task.xml" /tn "Display Off When Locked (Battery Only)"
if %errorlevel% equ 1 (
   if NOT EXIST "%dp0%\Display Off When Locked (Battery Only) Scheduled Task.xml" (
      cls & echo UNABLE TO CREATE TASK - "%dp0%\Display Off When Locked (Battery Only) Scheduled Task.xml" SHOULD BE CREATED BY THIS SCRIPT BUT DOES NOT EXIST. & echo. & pause
   ) else (
      cls & echo UNABLE TO CREATE TASK - ACCESS DENIED OR PROBLEM WITH "%dp0%\Display Off When Locked (Battery Only) Scheduled Task.xml" RESULTING IN UNSUCCESSFUL IMPORT. RUN SCRIPT AS ADMINISTRATOR, IF STILL GETTING THIS ERROR, CHECK XML FILE AGAINST "%dp0%\-Display Off When Locked (Battery Only) Scheduled Task- TEMPLATE.xml" FOR ERRORS. & echo. & set /p "user_input=Open files in notepad for comparison? [Y/N]"
      if NOT "%user_input%" == "Y" if NOT "%user_input%" == "y" (
         REM Do nothing
      ) else (
         start notepad.exe "%dp0%\Display Off When Locked (Battery Only) Scheduled Task.xml"
         if EXIST "%dp0%\-Display Off When Locked (Battery Only) Scheduled Task - TEMPLATE.xml" start notepad.exe "%dp0%\-Display Off When Locked (Battery Only) Scheduled Task - TEMPLATE.xml"
      )
   )
   cls & echo. & echo SUCCESS^^! & echo. & timeout /t 5
)

endlocal & exit /b

“MonOff (仅电池).bat”:

@echo off
setlocal enableDelayedExpansion

REM Only turn off monitor when on battery - Set scheduled task to run "nircmd.exe" (with quotes) with "monitor off" (without quotes) in argument to always turn monitor off
set "cmd=WMIC /NameSpace:\\root\WMI Path BatteryStatus Get PowerOnline"
%cmd% | find /i "true" > nul
if %errorlevel% EQU 1 (
   for /f "tokens=*" %%a in ('where /r .. nircmd.exe') do set nircmd=%%a
   start /b "" "!nircmd!" monitor off
)

endlocal & exit /b

“-锁定时显示关闭(始终)计划任务 - TEMPLATE.xml”:

<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.4" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
<RegistrationInfo>
<Date>2021-01-11T04:56:22.2785148</Date>
<Author>COMPUTER_NAME\Username</Author>
<URI>\Display Off When Locked (Always)</URI>
</RegistrationInfo>
<Triggers>
<SessionStateChangeTrigger>
<Enabled>true</Enabled>
<StateChange>SessionLock</StateChange>
</SessionStateChangeTrigger>
</Triggers>
<Principals>
<Principal id="Author">
<GroupId>S-1-5-32-544</GroupId>
<RunLevel>HighestAvailable</RunLevel>
</Principal>
</Principals>
<Settings>
<MultipleInstancesPolicy>StopExisting</MultipleInstancesPolicy>
<DisallowStartIfOnBatteries>false</DisallowStartIfOnBatteries>
<StopIfGoingOnBatteries>true</StopIfGoingOnBatteries>
<AllowHardTerminate>true</AllowHardTerminate>
<StartWhenAvailable>false</StartWhenAvailable>
<RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
<IdleSettings>
<StopOnIdleEnd>true</StopOnIdleEnd>
<RestartOnIdle>false</RestartOnIdle>
</IdleSettings>
<AllowStartOnDemand>true</AllowStartOnDemand>
<Enabled>true</Enabled>
<Hidden>false</Hidden>
<RunOnlyIfIdle>false</RunOnlyIfIdle>
<DisallowStartOnRemoteAppSession>false</DisallowStartOnRemoteAppSession>
<UseUnifiedSchedulingEngine>true</UseUnifiedSchedulingEngine>
<WakeToRun>false</WakeToRun>
<ExecutionTimeLimit>PT1H</ExecutionTimeLimit>
<Priority>7</Priority>
</Settings>
<Actions Context="Author">
<Exec>
<Command>"C:\Portable Apps\NirCmd x64 (Portable)\nircmd.exe"</Command>
<Arguments>monitor off</Arguments>
</Exec>
</Actions>
</Task>

“- 锁定时关闭显示(仅电池)计划任务 - 模板”:

<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.4" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
<RegistrationInfo>
<Date>2021-01-11T04:56:22.2785148</Date>
<Author>COMPUTER_NAME\Username</Author>
<URI>\Display Off When Locked (Battery Only)</URI>
</RegistrationInfo>
<Triggers>
<SessionStateChangeTrigger>
<Enabled>true</Enabled>
<StateChange>SessionLock</StateChange>
</SessionStateChangeTrigger>
</Triggers>
<Principals>
<Principal id="Author">
<GroupId>S-1-5-32-544</GroupId>
<RunLevel>HighestAvailable</RunLevel>
</Principal>
</Principals>
<Settings>
<MultipleInstancesPolicy>StopExisting</MultipleInstancesPolicy>
<DisallowStartIfOnBatteries>false</DisallowStartIfOnBatteries>
<StopIfGoingOnBatteries>true</StopIfGoingOnBatteries>
<AllowHardTerminate>true</AllowHardTerminate>
<StartWhenAvailable>false</StartWhenAvailable>
<RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
<IdleSettings>
<StopOnIdleEnd>true</StopOnIdleEnd>
<RestartOnIdle>false</RestartOnIdle>
</IdleSettings>
<AllowStartOnDemand>true</AllowStartOnDemand>
<Enabled>true</Enabled>
<Hidden>false</Hidden>
<RunOnlyIfIdle>false</RunOnlyIfIdle>
<DisallowStartOnRemoteAppSession>false</DisallowStartOnRemoteAppSession>
<UseUnifiedSchedulingEngine>true</UseUnifiedSchedulingEngine>
<WakeToRun>false</WakeToRun>
<ExecutionTimeLimit>PT1H</ExecutionTimeLimit>
<Priority>7</Priority>
</Settings>
<Actions Context="Author">
<Exec>
<Command>"C:\Portable Apps\MonOff (Display off When Locked)\MonOff (Battery Only).bat"</Command>
</Exec>
</Actions>
</Task>

可能存在问题的一件事是,当使用在一个系统上创建的任务作为在另一个系统上重新创建的模板时,它可能与新系统无关,因此可能不起作用。如果有人尝试这样做,那么了解它是否在他们的计算机上有效或是否是一个问题将会很有趣。如果是这样,我认为解决方案是

schtasks /Change /RU {username/Administrator}

尽管这要求输入密码,所以脚本不会完全自动化,并且如果管理员密码为空,我猜测在大多数用户/消费者系统上都是如此(用户(希望)有一个密码,但这与管理员密码不同,通常不会单独设置),它表示该任务可能无法根据组策略运行。

相关内容