Schedule PSScheduledJob with a RepetitionInterval of less than 1 minute

When trying to define a RepitionInterval on a schedule job of less than one minute you receive the following error message:

Task Scheduler
The repetition interval value must be 1 minute or greater and less than or equal to 31 days.

However, there is a workaround on this: you define additional triggers that fire every minute and have them start eg. every 15 seconds:

$tsi = New-TimeSpan -Minutes 1;
$tsd = [timespan]::MaxValue;
$jt00 = New-JobTrigger -Once -At "00:00:00" -RepetitionInterval $tsi -RepetitionDuration $tsd;
$jt15 = New-JobTrigger -Once -At "00:00:15" -RepetitionInterval $tsi -RepetitionDuration $tsd;
$jt30 = New-JobTrigger -Once -At "00:00:30" -RepetitionInterval $tsi -RepetitionDuration $tsd;
$jt45 = New-JobTrigger -Once -At "00:00:45" -RepetitionInterval $tsi -RepetitionDuration $tsd;
$sjo = New-ScheduledJobOption -MultipleInstancePolicy Parallel;
$sj = Register-ScheduledJob -Name "RunVeryOften" -FilePath "X:\RunVeryOften.ps1" -ScheduledJobOption $sjo -Trigger $jt00 -Credential $cred;
Add-JobTrigger -id $ -Trigger $jt15;
Add-JobTrigger -id $ -Trigger $jt30;
Add-JobTrigger -id $ -Trigger $jt45;

PS > $sj.JobTriggers;

Id Frequency Time DaysOfWeek Enabled
-- --------- ---- ---------- -------
1 Once 2013-04-20 00:00:00 True
2 Once 2013-04-20 00:00:15 True
3 Once 2013-04-20 00:00:30 True
4 Once 2013-04-20 00:00:45 True

There is a maximum of 48 triggers you can define for any one PowerShell scheduled job (in PowerShell v3).

There are some drawbacks with that approach, however:

  1. It is quite resource intensive. You have to fire up a complete PowerShell session with modules and the like.
  2. Startup of scheduled job is quite slow. So do not expect your jobs to execute at “:00”, “:15”, … but maybe 5 to 10 seconds later.
  3. If you do not define “Parallel” for the “MultipleInstancePolicy” option your jobs might be skipped anyway, because one of your jobs might still be running.
  4. You will need a much higher execution history than the default of 32 to get some results that go back in time longer than just a couple of minutes.

So in practice is might be much better to have one job that spawns multiple ChildJobs that sleep for an amount of time (or have it processed in one job altogether).

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: