首页 > 解决方案 > 通过 Powershell 将计划任务部署到多台计算机

问题描述

我正在尝试将计划任务部署到网络中的多台计算机。所以任务的作用是当计算机被锁定时,它会在空闲 1 小时 + 后重新启动计算机。我似乎无法使用该-xml参数进行部署。我在下面包含的错误。

<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.4" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
  <RegistrationInfo>
    <Date>2020-05-02T14:35:05.1241005</Date>
    <Author>ANT\user</Author>
    <URI>\Reboot</URI>
  </RegistrationInfo>
  <Triggers>
    <SessionStateChangeTrigger>
      <Enabled>true</Enabled>
      <StateChange>SessionLock</StateChange>
      <Delay>PT1H</Delay>
    </SessionStateChangeTrigger>
  </Triggers>
  <Principals>
    <Principal id="Author">
      <UserId>S-1-5-21-1407069837-2091007605-538272213-32053677</UserId>
      <LogonType>Password</LogonType>
      <RunLevel>HighestPrivilege</RunLevel>
    </Principal>
  </Principals>
  <Settings>
    <MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>
    <DisallowStartIfOnBatteries>true</DisallowStartIfOnBatteries>
    <StopIfGoingOnBatteries>true</StopIfGoingOnBatteries>
    <AllowHardTerminate>true</AllowHardTerminate>
    <StartWhenAvailable>false</StartWhenAvailable>
    <RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
    <IdleSettings>
      <Duration>PT1H</Duration>
      <WaitTimeout>PT1H</WaitTimeout>
      <StopOnIdleEnd>true</StopOnIdleEnd>
      <RestartOnIdle>true</RestartOnIdle>
    </IdleSettings>
    <AllowStartOnDemand>false</AllowStartOnDemand>
    <Enabled>true</Enabled>
    <Hidden>false</Hidden>
    <RunOnlyIfIdle>true</RunOnlyIfIdle>
    <DisallowStartOnRemoteAppSession>false</DisallowStartOnRemoteAppSession>
    <UseUnifiedSchedulingEngine>true</UseUnifiedSchedulingEngine>
    <WakeToRun>false</WakeToRun>
    <ExecutionTimeLimit>PT72H</ExecutionTimeLimit>
    <Priority>7</Priority>
  </Settings>
  <Actions Context="Author">
    <Exec>
      <Command>%SystemRoot%\system32\shutdown.exe</Command>
      <Arguments>-r -f -t 10</Arguments>
    </Exec>
  </Actions>
</Task>
$msg = "Enter the username and password that will run the task";
$credential = $Host.UI.PromptForCredential("Task username and password",$msg,"$env:userdomain\$env:username",$env:userdomain)

$username = $credential.UserName
$password = $credential.GetNetworkCredential().Password

Register-ScheduledTask -Xml (Get-Content "C:\Reboot.xml" | Out-String) -TaskName "Reboot" -RunLevel Highest -User $username -Password $password

PS$ 错误返回

Register-ScheduledTask : Parameter set cannot be resolved using the specified named parameters.
At C:\Users\ermn\Desktop\P$.ps1:11 char:1
+ Register-ScheduledTask -Xml (Get-Content "C:\Reboot.xml" | Out-String ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Register-ScheduledTask], ParameterBindingException
    + FullyQualifiedErrorId : AmbiguousParameterSet,Register-ScheduledTask

标签: powershell

解决方案


您最初发布的错误说,您在 Register-ScheduledTask cmdlet 中使用了不能一起使用的参数。

根据该 cmdlet 的文档,在使用 xml 参数时,您似乎无法使用 runlevel 参数。

此外,您已经在 xml 中声明了运行级别。从您的命令中删除运行级别参数。


推荐阅读