Trigger Task Scheduler Working from powershell

I want to create a task that runs between the hours from 6:00 to 21:00 from Monday to Friday and the triggers in 15 minutes, and the task should be completed if it lasts more than 10 minutes.

I tried the code below:

$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 goals except termination of work if it lasts more than 10 minutes. I am getting below the 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 get through this problem. Thanks in advance.

+4
source share
1 answer

I think the executelimit task settings are what you are looking for:

$task.Settings.ExecutionTimeLimit =  'PT30M'

command line version of the same:

 $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

+2

Source: https://habr.com/ru/post/1679017/


All Articles