触发任务调度程序来自powershell的作业

时间:2017-06-12 04:57:34

标签: windows powershell scheduled-tasks taskscheduler

我想创建一个在星期一到星期五的早上6点到晚上9点之间运行的作业,并以15分钟的间隔触发,如果运行时间超过10分钟,作业应该终止。

我尝试过以下代码:

$action = New-ScheduledTaskAction -Execute Powershell.exe
$trigger = New-ScheduledTaskTrigger -Weekly -At 6:30AM -DaysOfWeek 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday' 
$task = Register-ScheduledTask -TaskName "TaskName" -Trigger $trigger -Action $action -RunLevel Highest 
$task.Triggers.ExecutionTimeLimit = 'PT30M'
$task.Triggers.Repetition.Duration = 'PT15H' 
$task.Triggers.Repetition.Interval= 'PT15M'
$task.Triggers.Repetition.Duration = 'PT15H' 
$task | Set-ScheduledTask -User "UserName" -Password "Password"

如果超过10分钟,我已完成所有其他目标,但终止工作除外。 我收到了以下错误。

The property 'ExecutionTimeLimit' cannot be found on this object. Verify that the property exists and can be set.
At line:4 char:1
+ $task.Triggers.ExecutionTimeLimit = 'PT10M'
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : PropertyAssignmentException

请帮我解决这个问题。谢谢。

1 个答案:

答案 0 :(得分:3)

我认为任务设置的执行限制就是你要找的东西:

$task.Settings.ExecutionTimeLimit =  'PT30M'

相同的命令行开发版:

 $settings = New-ScheduledTaskSettingsSet -ExecutionTimeLimit (New-TimeSpan -Minutes 30)

 Set-ScheduledTask -TaskName 'taskname' -Settings $settings

有关该属性的参考:https://technet.microsoft.com/en-us/library/cc722178(v=ws.11).aspx

关于触发器执行限制和任务设置执行限制的有用讨论:https://superuser.com/questions/506662/what-is-the-difference-between-stop-the-task-if-it-runs-longer-than-inside-tri