powershell - 通过 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
解决方案
您最初发布的错误说,您在 Register-ScheduledTask cmdlet 中使用了不能一起使用的参数。
根据该 cmdlet 的文档,在使用 xml 参数时,您似乎无法使用 runlevel 参数。
此外,您已经在 xml 中声明了运行级别。从您的命令中删除运行级别参数。
推荐阅读
- javascript - 当用户在地址栏 Angular 上单击输入时检测事件
- php - 在 Laravel 中更新大量条目的最佳方法
- powershell - 在 Windows 10 上通过 ssh 在 cygwin 上获得 rsync 的神奇组合
- google-cloud-firestore - 用户喜欢 Firestore 中的结构以避免高读取次数
- c# - “字符串 []”不包含“包含”的定义
- npm - npm 6.3.0 --only=prod install devDependencies 也
- c# - 需要来自 Json 字符串的所有唯一键名
- vba - 用户窗体自动调整请求宽度/高度的 80% 并更改纵横比
- javascript - 将文本发送到特定频道的命令
- jenkins - 声明式 Jenkins 流水线中的检查点