Reputation: 785
I am currently trying to set up a Windows Scheduled Task from Powershell, setting the RestartOnFailure attribute.
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)
however, when trying to set
$taskDefinition.XmlText = $taskxml
I get the following error
Exception setting "XmlText": "(1,2)::ERROR: incorrect document syntax"
At line:1 char:1
+ $taskDefinition.XmlText = $taskxml
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], SetValueInvocationException
+ FullyQualifiedErrorId : CatchFromBaseAdapterSetValueTI
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>
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>
Upvotes: 3
Views: 4053
Reputation: 5570
You are trying to pass an xml object where powershell is expecting a string.
change the line that errors to :
$taskDefinition.XmlText = $taskxml.OuterXml
Powershell: Convert XML to String
Upvotes: 3