简体   繁体   中英

Trigger task scheduler Job from powershell

I want to create a job that runs between the hours of 6 AM and 9 PM Monday through Friday and triggers in an interval of 15 min and the job should terminate if it runs longer than 10 minutes.

I have tried the below code:

$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"

I have achieved all other objectives except the termination of job if it runs more than 10 min. I am getting below error.

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

Please help me to over come this issue.Thanks in advance.

I think task settings executionlimit is what you are looking for :

$task.Settings.ExecutionTimeLimit =  'PT30M'

a commandlet version of the same:

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

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

for reference on the property : https://technet.microsoft.com/en-us/library/cc722178(v=ws.11).aspx

A useful discussion regarding trigger executionlimit and task setting executionlimit : https://superuser.com/questions/506662/what-is-the-difference-between-stop-the-task-if-it-runs-longer-than-inside-tri

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM