Powershell New-ScheduledTaskTrigger recurring task with -RepetitionInterval

NealWalters picture NealWalters · Sep 15, 2014 · Viewed 21.4k times · Source

I'm finding the errors related to ScheduledTask cmdles give extremely cryptic errors. I want to schedule a test task to run every 1 minute and repeat forever. Here's what I'm trying, but no success:

cls
#$Action = New-ScheduledTaskAction -Execute "e:\Scripts\TestClusteredTask.cmd"
$DurationTimeSpan = New-TimeSpan -Minutes 1 
$DurationTimeSpanIndefinite = ([TimeSpan]::MaxValue) 
$DurationTempTest = New-TimeSpan -Days 1000
$Trigger = New-ScheduledTaskTrigger -Daily -At "00:01" -RepetitionInterval $DurationTimeSpan -RepetitionDuration $DurationTempTest  

also tried this:

$Trigger = New-ScheduledTaskTrigger -Daily -At "00:01" -RepetitionInterval $DurationTimeSpan -RepeatIndefinitely 

Error Output:

New-ScheduledTaskTrigger : Parameter set cannot be resolved using the specified named parameters.
At C:\Users\nwalters\Documents\AddTestClusteredTask.ps1:6 char:12
+ $Trigger = New-ScheduledTaskTrigger -Daily -At "00:01" -RepetitionInterval $Dura ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [New-ScheduledTaskTrigger], ParameterBindingException
    + FullyQualifiedErrorId : AmbiguousParameterSet,New-ScheduledTaskTrigger

I'm referencing this web page for instructions. http://technet.microsoft.com/en-us/library/hh849759.aspx

Powerhsell Version 4 on Win 2012/R2

Answer

Robert R. picture Robert R. · Jan 11, 2017

I found out that you can modify the Trigger before you link it to the task. My Script generates a Task with a Trigger at 8:00 o Clock, every day. The tasks is repeaded every 60 Minutes (1 hour) with a random delay of 30 minutes:

$Prog = $env:systemroot + "\system32\WindowsPowerShell\v1.0\powershell.exe"
$Opt = "-nologo -noninteractive -noprofile -ExecutionPolicy BYPASS -file \\mydomain.local\NETLOGON\Monitoring\monitoring.ps1"
$Action = New-ScheduledTaskAction -Execute $Prog -Argument $Opt 
$Trigger = New-ScheduledTaskTrigger -Daily -DaysInterval 1 -RandomDelay "00:30" -At "08:00"
$Trigger.Repetition = $(New-ScheduledTaskTrigger -Once -RandomDelay "00:30" -At "08:00" -RepetitionDuration "12:00" -RepetitionInterval "01:00").Repetition
$Principal = New-ScheduledTaskPrincipal -UserId "SYSTEM" -LogonType ServiceAccount -RunLevel Highest
$Task = Register-ScheduledTask -Action $Action -Trigger $Trigger -Principal $Principal -TaskName "Monitor-PS1-V001" -Description "Starts Monitoring-Script" -Force

I googled around and found no nice solution. Hopefully somebody can use my "idea"

Greetings Robert