我目前正在尝试从 Powershell 设置 Windows 计划任务,设置 RestartOnFailure 属性。
查看任务设置对象 ( https://msdn.microsoft.com/en-us/library/windows/desktop/aa383480(v=vs.85).aspx ),这似乎无法直接执行,所以我尝试通过获取任务 XML、手动设置此属性,然后将 xml 保存回任务来实现.
这是我当前设置任务的代码,
$Hostname = $Env:computername
$service = new-object -com("Schedule.Service")
$service.Connect($Hostname)
$taskDefinition = $service.NewTask(0)
$taskRunAsuser = "Domain\Username"
$taskRunasUserPwd = "Password_ClearText"
$rootFolder = $service.GetFolder("\")
$regInfo = $taskDefinition.RegistrationInfo
$regInfo.Description = 'BounceFailedAppStartup'
$regInfo.Author = $taskRunAsuser
$settings = $taskDefinition.Settings
$settings.Enabled = $True
$settings.StartWhenAvailable = $True
$settings.Hidden = $False
$triggers = $taskDefinition.Triggers
$trigger = $triggers.Create(2)
$trigger.StartBoundary = "2015-08-10T04:15:00"
$trigger.DaysInterval = 1
$trigger.Id = "DailyTriggerId"
$trigger.Enabled = $True
$Action = $taskDefinition.Actions.Create(0)
$Action.Path = 'powershell.exe'
[xml] $taskxml = $taskDefinition.XmlText
$settingsnode = $taskxml.GetElementsByTagName("Settings")
$restartonFaliure = $taskxml.CreateElement("RestartOnFailure", $taskxml.DocumentElement.NamespaceURI)
$interval = $taskxml.CreateElement("Interval", $taskxml.DocumentElement.NamespaceURI)
$count = $taskxml.CreateElement("Count", $taskxml.DocumentElement.NamespaceURI)
$interval.InnerText = "PT15M"
$count.InnerText = 10
$restartonFaliure.AppendChild($interval)
$restartonFaliure.AppendChild($count)
$taskxml.Task.Settings.AppendChild($restartonFaliure)
$taskDefinition.XmlText = $taskxml
$rootFolder.RegisterTaskDefinition( 'ResartAppPool', $taskDefinition, 6, $taskRunAsuser , $taskRunasUserPwd , 0)
然而,当试图设置
$taskDefinition.XmlText = $taskxml
出现以下错误
Exception setting "XmlText": "(1,2)::ERROR: incorrect document syntax"
At line:1 char:1
+ $taskDefinition.XmlText = $taskxml
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], SetValueInvocationException
+ FullyQualifiedErrorId : CatchFromBaseAdapterSetValueTI
这是导入失败的任务的 xml
<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
<RegistrationInfo>
<Author>Domain\Username</Author>
<Description>BounceFailedAppStartup</Description>
</RegistrationInfo>
<Triggers>
<CalendarTrigger id="DailyTriggerId">
<StartBoundary>2015-08-10T04:15:00</StartBoundary>
<Enabled>true</Enabled>
<ScheduleByDay>
<DaysInterval>1</DaysInterval>
</ScheduleByDay>
</CalendarTrigger>
</Triggers>
<Settings>
<MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>
<DisallowStartIfOnBatteries>true</DisallowStartIfOnBatteries>
<StopIfGoingOnBatteries>true</StopIfGoingOnBatteries>
<AllowHardTerminate>true</AllowHardTerminate>
<StartWhenAvailable>true</StartWhenAvailable>
<RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
<IdleSettings>
<Duration>PT10M</Duration>
<WaitTimeout>PT1H</WaitTimeout>
<StopOnIdleEnd>true</StopOnIdleEnd>
<RestartOnIdle>false</RestartOnIdle>
</IdleSettings>
<AllowStartOnDemand>true</AllowStartOnDemand>
<Enabled>true</Enabled>
<Hidden>false</Hidden>
<RunOnlyIfIdle>false</RunOnlyIfIdle>
<WakeToRun>false</WakeToRun>
<ExecutionTimeLimit>PT72H</ExecutionTimeLimit>
<Priority>7</Priority>
<RestartOnFailure>
<Interval>PT15M</Interval>
<Count>10</Count>
</RestartOnFailure>
</Settings>
<Actions>
<Exec>
<Command>powershell.exe</Command>
</Exec>
</Actions>
</Task>
这里是已在 Task Scheduler 中的任务的 xml,其中已在 UI 中设置了此属性
<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.3" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
<RegistrationInfo>
<Author>DOMAIN\User</Author>
</RegistrationInfo>
<Triggers>
<CalendarTrigger>
<StartBoundary>2015-08-10T04:15:00</StartBoundary>
<Enabled>true</Enabled>
<ScheduleByDay>
<DaysInterval>1</DaysInterval>
</ScheduleByDay>
</CalendarTrigger>
</Triggers>
<Principals>
<Principal id="Author">
<RunLevel>LeastPrivilege</RunLevel>
<UserId>Domain\user</UserId>
<LogonType>InteractiveToken</LogonType>
</Principal>
</Principals>
<Settings>
<MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>
<DisallowStartIfOnBatteries>true</DisallowStartIfOnBatteries>
<StopIfGoingOnBatteries>false</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>false</UseUnifiedSchedulingEngine>
<WakeToRun>false</WakeToRun>
<ExecutionTimeLimit>P3D</ExecutionTimeLimit>
<Priority>7</Priority>
<RestartOnFailure>
<Interval>PT15M</Interval>
<Count>19</Count>
</RestartOnFailure>
</Settings>
<Actions Context="Author">
<Exec>
<Command>powershell.exe</Command>
</Exec>
</Actions>
</Task>
最佳答案
您正试图在 powershell 需要字符串的地方传递一个 xml 对象。
将错误行更改为:
$taskDefinition.XmlText = $taskxml.OuterXml
关于xml - 设置RestartOnFailure后无法在Powershell中导入计划任务xml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31922050/