设置 RestartOnFailure 后无法在 Powershell 中导入计划任务 xml [英] Unable to import scheduled task xml in Powershell after setting RestartOnFailure

查看:60
本文介绍了设置 RestartOnFailure 后无法在 Powershell 中导入计划任务 xml的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试从 Powershell 设置 Windows 计划任务,设置 RestartOnFailure 属性.

I am currently trying to set up a Windows Scheduled Task from Powershell, setting the RestartOnFailure attribute.

查看任务设置对象(https://msdn.microsoft.com/en-us/library/windows/desktop/aa383480(v=vs.85).aspx),这似乎不能直接做到,所以我尝试通过获取任务 XML,手动设置此属性,然后将 xml 保存回任务来完成此操作.

Looking at the task settings object (https://msdn.microsoft.com/en-us/library/windows/desktop/aa383480(v=vs.85).aspx), this does not seem possible to do directly, so I am trying to do it by taking the task XML, manually setting this attribute, then saving the xml back to the task.

这是我当前设置任务的代码,

Here is my current code for setting up the task,

$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

$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

This is the xml for the task that is failing to import

<?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 中设置

and here is the xml of a task already in Task Scheduler where this property has been set in the 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 需要一个字符串.

You are trying to pass an xml object where powershell is expecting a string.

将出错的行更改为:

$taskDefinition.XmlText = $taskxml.OuterXml

Powershell:将 XML 转换为字符串

这篇关于设置 RestartOnFailure 后无法在 Powershell 中导入计划任务 xml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆