4

I have a problem with writing a script to automate the creation of Scheduled Tasks in Windows.

After much Googling, I am unable to deduce which parameters to the command-line utility SCHTASKS.EXE control the setting "Stop the task if it runs for hours."

mseery
  • 141
  • 1
  • 3

2 Answers2

2

I cannot tell you that it is possible to automate this with schtasks. However, it can be done in powershell.

Once you create your task with schtasks (which you can do in powershell), you can use code like this to update the execution time limit:

$task = Get-ScheduledTask "MyTaskName"
$task.Settings #Optional to see what is going on
$task.Settings.ExecutionTimeLimit = "PT0S"
$task.Settings #Optional to see how it changed
Set-ScheduledTask $task

The key is knowing the PT0S setting. P3D is the default 3 days. PT0S unchecks the option and allows the task to run forever. If you want a definitive answer on a specific duration, set the options the way you want them in a task using the UI, then export the task. If you look at the resulting XML file, you can see the setting for ExecutionTimeLimit.

1

Your best would would probably be the /DU switch.

/DU duration

A value that specifies the duration to run the task. The time format is HH:mm (24-hour time). For example, 14:50 specifies 2:50PM. This is not applicable with /ET and for the following schedule types: ONSTART, ONLOGON, ONIDLE, and ONEVENT. For /V1 tasks (Task Scheduler 1.0 tasks), if /RI is specified, then the duration default is one hour.

Windows XP: This option is not available.

Quoted from MSDN. Why that information isn't in the TechNet Library page for schtasks is beyond me.

Wesley
  • 32,690
  • 9
  • 82
  • 117
  • unfortunately this is not the case. /du does not change this settings for an existing task if you do schtasks /change /tn taskname /du x:xx – Vitas Nov 25 '15 at 08:02