Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell v3 New-JobTrigger daily with repetition

Tags:

powershell

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:

New-JobTrigger [-Daily] -At <DateTime> [-DaysInterval <Int32> ] [-RandomDelay <TimeSpan> ] [ <CommonParameters>]

If I use the Once parameter-set, I can't have the Daily option

New-JobTrigger [-Once] -At <DateTime> [-RandomDelay <TimeSpan> ] [-RepetitionDuration <TimeSpan> ] [-RepetitionInterval <TimeSpan> ] [ <CommonParameters>]

What I need, but obviously doesn't work, is a mix between the two. For example:

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?

like image 599
santi Avatar asked Oct 07 '12 12:10

santi


1 Answers

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.

$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
like image 54
Michael Thomas Avatar answered Sep 28 '22 06:09

Michael Thomas