9

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?

santi
  • 487
  • 2
  • 4
  • 10

8 Answers8

6

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
  • is that cmdlet part of native PowerShell ? or is it in the PSCX or something? –  Nov 30 '12 at 14:54
  • 3
    Yeah, except it doesn't do what the question was asking. He's trying to create a daily trigger that runs hourly. How is this marked as accepted? This to me looks like you're scheduling it to run for one day, every 5 minutes. Am I missing something? – Kyle Gobel Jan 11 '16 at 19:33
  • This doesn't schedule a daily task for every 5 minutes. This creates a trigger for one day and for that day repeats it every 5 mintues. It won't run after the day expires, hence, it doesn't run daily. – unrealsoul007 Jul 02 '18 at 21:54
1

I have been working on this issue all day and i finally have the answer. You cant. Not using those cmdlets anyway. At first i thought this might work as a workaround:

$Job = Register-ScheduledJob -Name "YourJobName"  {Gci};
$RepeatTrigger = New-JobTrigger -Once -At (Get-Date 10:00).ToShortTimeString()  -     RepetitionInterval (New-TimeSpan -Hours 1) -RepetitionDuration (New-TimeSpan -Hours 7);
$RepeatTrigger.Frequency = [Microsoft.PowerShell.ScheduledJob.TriggerFrequency]::Daily;
Add-JobTrigger -InputObject $Job -Trigger $RepeatTrigger

However, it doesnt. Using trace-command it looks like it validates the attributes depending on the parameter set. The same is true when using schtasks:

schtasks /create /tn YourTask /tr notepad.exe /sc Hourly 

It will create it hourly but again it will have to be a once off task. By the looks of things there is a .Net wrapper for the for the COM API which is probably going to be your best bet if your new to PoSH. You can find it on CodePlex.

1

I know about Add-JobTrigger, but I don't understand how to achieve my specific requirement: run a job daily, repeating itself every hour, for a specific duration. For example: every day, at 10 AM, repeating every 1 hour until 5 PM. – scarmuega Oct 7 '12 at 18:14

Create the job with Register-ScheduledJob. Then add the daily triggers. It would be great if we could do this, but the -Daily doesn't work in PowerShell 4.0. Read on, I have an alternative for you.

# Technet: http://technet.microsoft.com/en-us/library/hh849759.aspx
# To create a repeating schedule, use the Once parameter with the RepetitionDuration and RepetitionInterval parameters.
# -RepetitionInterval Repeats the job at the specified time interval. For example, if the value of this parameter is 2 hours, the job is triggered every two hours.
#  The default value, 0, does not repeat the job.
# -RepetitionDuration Repeats the job until the specified time expires. The repetition frequency is determined by the value of the RepetitionInterval parameter. 
#  For example, if the value of RepetitionInterval is 5 minutes and the value of RepetitionDuration is 2 hours, the job is triggered every five minutes for two hours.
#
$trigger = New-JobTrigger -Daily -At 10:00 -RepetitionDuration 7:00:00 -RepetitionInterval 1:00:00 -DaysofWeek 
Register-ScheduledJob -Name Test -Trigger $trigger -ScriptBlock {}

You can just organize your job so that it has a trigger for each hour that you want.

$trigger4am  = New-JobTrigger -Daily -At 04:10
Register-ScheduledJob -Name ArchiveProd -Trigger $trigger4am -ScriptBlock {E:\ArchiveProd.ps1}

$trigger8am  = New-JobTrigger -Daily -At 08:10
$trigger12pm = New-JobTrigger -Daily -At 12:10
$trigger6pm  = New-JobTrigger -Daily -At 18:10
Add-JobTrigger -Trigger $trigger8am  -Name ArchiveProd
Add-JobTrigger -Trigger $trigger12pm -Name ArchiveProd
Add-JobTrigger -Trigger $trigger6pm  -Name ArchiveProd

Hope that is what you needed. It works nice for my situation, although I would prefer to just add the Daily parameter. Maybe someobody can improve on this for me?

Jason
  • 43
  • 6
  • The -Trigger parameter of Register/Set-ScheduledJob takes multiple triggers, so just script that baby: `-Trigger ((4..9 | % { $h = $_; '00', '15', '30', '45' | % { "${h}:${_}" } }) + '10:00' | % { New-JobTrigger -Daily -At $_ })` will give you triggers for every 15 minutes between 4am and 10am. – Bergius Dec 04 '15 at 20:55
1

A late answer, but I started with this question then searched more and found a solution at this link, Petri.com

$principal = New-ScheduledTaskPrincipal -UserID MyDomain\MyGMSA$ -LogonType Password -RunLevel Highest
$action = New-ScheduledTaskAction  "E:\DoSomething"
$when = New-ScheduledTaskTrigger -Daily -At "00:02" -DaysInterval 1

Register-ScheduledTask -TaskName MyNewTask–Action $action –Principal $principal -Trigger  $when

$t = Get-ScheduledTask -TaskName MyNewTask

$t.Triggers.repetition.Interval = "PT15M"    #every 15 mins
$t.Triggers.repetition.Duration = "PT7H30M" #for 7 hours 30
$t | Set-ScheduledTask
gbn
  • 422,506
  • 82
  • 585
  • 676
0

Try using Add-JobTrigger to add multiple job triggers to a scheduled job.

Keith Hill
  • 194,368
  • 42
  • 353
  • 369
  • 1
    I know about Add-JobTrigger, but I don't understand how to achieve my specific requirement: run a job daily, repeating itself every hour, for a specific duration. For example: every day, at 10 AM, repeating every 1 hour until 5 PM. – santi Oct 07 '12 at 18:14
0
clear
Import-Module TaskScheduler 

# Create a new task 
$task = New-Task 


Add-TaskAction -Task $task -Path "C:\Windows\notepad.exe"   
Add-TaskTrigger -Task $task -Daily -At "06:00" -Repeat "05:00" -For "12:00" 

# Register the task 
Register-ScheduledTask -Name "PowerShell - MyScript.ps1" -Task $task

Get-ScheduledTask PowerShell* | select -Property Name
# Remove-Task -n Power*
majkinetor
  • 8,730
  • 9
  • 54
  • 72
  • http://blog.powershell.no/2012/05/28/working-with-scheduled-tasks-from-windows-powershell/ copy pasta much ? and it isnt even the solution to this question. Please try solution before posting. –  Oct 08 '12 at 11:49
0

You can create multiple triggers, see below:

$job = Register-ScheduledJob -Name "Test1" -FilePath "C:\temp\script.ps1"
$triggers = New-Object System.Collections.Generic.List[Microsoft.PowerShell.ScheduledJob.ScheduledJobTrigger]
$times = ("08:00", "09:00", "10:00", "11:00", "12:00", "14:00", "15:00", "16:00", "17:00", "19:00", "22:00")
foreach($t in $times){
$triggers.Add((New-JobTrigger -Daily -At $t))
}
$job.AddTriggers($triggers, $true)

Or easier (but slower)

$MyJob = Register-ScheduledJob -Name "MyJob" -FilePath "C:\temp\test.ps1"
$times = ("08:00", "09:00", "10:00", "11:00", "12:00", "14:00", "15:00", "16:00", "17:00", "19:00", "22:00")
foreach($t in $times){$MyJob | Add-JobTrigger -trigger (New-JobTrigger -Daily -At $t)}
Mike Twc
  • 2,230
  • 2
  • 14
  • 19
0

When you are creating a Scheduled-Job with "Register-ScheduledJob", you can't configure a daily execution in combination with a repetition interval. Even if you modify the properties "RepetitionInterval" and "RepetitionDuration" of the ScheduledJobTrigger object manually, the trigger will be configured to execute daily OR once with the repetition. I managed to configure it by obtaining the corresponding task for the job through the Get-ScheduledTask method and modifying the trigger there:

    $trigger = New-JobTrigger -Weekly -WeeksInterval 1 -At "06:00" -DaysOfWeek Monday,Tuesday,Wednesday,Thursday,Friday     
    Register-ScheduledJob -Name $cronjobName -Trigger $trigger -ScriptBlock $scriptBlock
    $task = Get-ScheduledTask -TaskName $cronjobName
    $trigger = $task.Triggers
    $trigger.Repetition.Interval = "PT10M"
    $trigger.Repetition.Duration = "PT12H"
    Set-ScheduledTask -TaskPath $task.TaskPath -TaskName $cronjobName -Trigger $trigger
Christof
  • 9
  • 2