设置 RestartOnFailure 后无法在 Powershell 中导入计划任务 xml

Unable to import scheduled task xml in Powershell after setting RestartOnFailure

我目前正在尝试从 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 = $任务xml

我收到以下错误

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>   

这是任务计划程序中已有任务的 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>

您正在尝试传递一个 xml 对象,其中 powershell 需要一个字符串。

将错误行更改为:

$taskDefinition.XmlText = $taskxml.OuterXml

Powershell: Convert XML to String