Question:
Powershell v3 comes with all these new job-scheduling cmdlets. They look great, but I’m having trouble creating a specific trigger. I need to run a job daily, repeating itself every hour, for a specific interval.
Using the Task-Scheduler UI is straightforward, but I can’t find my way around the New-JobTrigger cmdlet.
If I use the Daily parameter-set, I don’t have the Repetition option:
1 2 |
New-JobTrigger [-Daily] -At |
If I use the Once parameter-set, I can’t have the Daily option
1 2 |
New-JobTrigger [-Once] -At |
What I need, but obviously doesn’t work, is a mix between the two. For example:
1 2 |
New-JobTrigger -Daily -At xxxx -RepetitionDuration (New-TimeSpan -Hours 5) -RepetitionInterval (New-TimeSpan -Hours 1) |
Is it possible? Maybe mixing several triggers for the same job?
Answer:
This works for me:
Note – I know that the original question was related to New-JobTrigger
The intent was to provide a possible solution that may be translatable
to New-JobTrigger as well given the issue applies to both.
1 2 3 4 5 6 7 |
$A = New-ScheduledTaskAction -execute "powershell" -argument "-nologo -noprofile -noninteractive C:\MyScript.ps1" $T = New-ScheduledTaskTrigger -Once -At 03:00AM Register-ScheduledTask -TaskName StartBasicServices_Local -Trigger $T -Action $A -description "MyScript" -User "NT AUTHORITY\SYSTEM" -RunLevel 1 $T.RepetitionInterval = (New-TimeSpan -Minutes 5) $T.RepetitionDuration = (New-TimeSpan -Days 1) Set-ScheduledTask StartBasicServices_Local -Trigger $T |