2017-06-12 102 views
4

我想要創建一個作業,該作業在週一至週五上午6點到晚上9點之間運行,並以15分鐘爲間隔觸發,如果運行時間超過10分鐘,則該作業應該終止。從powershell觸發任務計劃程序作業

我曾嘗試下面的代碼:

$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 

請幫我解決這個問題。提前感謝。

回答

2

我覺得任務設置executionlimit是你在找什麼:

$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

+0

謝謝Abhijith。它有幫助 – Venkatakrishnan

相關問題