
close
close
In an earlier article, I demonstrated how to use PowerShell’s Scheduled Jobs feature. Although I find that this a very useful tool, it is not perfect. One limitation is that you cannot setup a scheduled job to repeat itself.
By that, I mean that you can’t schedule something to run once a week starting at 10:00 AM, where you want the job to repeat every 15 minutes for an hour. If you look at help for New-JobTrigger, you’ll see that you can use repetition.
New-JobTrigger information in Windows PowerShell. (Image Credit: Jeff Hicks)
Repetition settings for New-JobTrigger in Windows PowerShell. (Image Credit: Jeff Hicks)
advertisment
$trigger.RepetitionDuration = (new-timespan -minutes 10 )
$trigger.RepetitionInterval = (new-timespan -Minutes 1)
Register-Scheduledjob -Name 'Weekly Rep Test A' -MaxResultCount 4 -ScriptBlock $a -Trigger $trigger
Repetition settings in Task Scheduler. (Image Credit: Jeff Hicks)
$t = get-scheduledtask -TaskName "Weekly Rep Test A"
$t.Triggers.repetition.Duration = 'PT60M'
$t.Triggers.repetition.Interval = 'PT01M'
$t | Set-ScheduledTask
Updated repetition values in Task Scheduler. (Image Credit: Jeff Hicks)
$name = "Daily Rep Test" Register-ScheduledJob -Name $name -Trigger $trigger -ScriptBlock $action -MaxResultCount 4 Start-Sleep -Seconds 5 $task = Get-ScheduledTask -TaskName $Name $task.Triggers.repetition.Duration = 'PT10M'
This is something you can try yourself. You might need to change the path to the text file. The first few lines create a typical PowerShell scheduled job that is simply going to append the date and time to a text file. The job is scheduled to run for five minutes on a daily basis. After the scheduled job is created, I'm pausing a few seconds to give the Task Scheduler time to update. Next, I go back and get the scheduled job with Get-ScheduledTask to update the repetition settings so that my PowerShell command runs every minute for 10 minutes.Interestingly, the scheduled job cmdlets don't recognize these settings.Updating repetition settings so our PowerShell command runs every minute for 10 minutes. (Image Credit: Jeff Hicks)
advertisment
But these settings are present when viewed from task scheduler.The scheduled job cmdlets do not recognize our repetition settings. (Image Credit: Jeff Hicks)
This is because the scheduled job definition is stored in an XML file under $env:LocalAppData\Microsoft\Windows\PowerShell\ScheduledJobs. Although you could manually modify the XML, I don't think it would affect the Task Scheduler, which is really the driving force here. I think what I have come up with is the best alternative. Of course, you could simply use the Task Scheduler cmdlets to create a PowerShell related task from the very beginning. You would lose the ability to use scheduled job cmdlets, but that might be okay for your purposes. If you are stuck running Windows 7, it is unlikely that you'll have the ScheduledTasks module, so you'll have to manually configure repetition intervals. I hope you'll let me know if this solves your problem.Our settings are visible when viewed from the Task Scheduler. (Image Credit: Jeff Hicks)
More in PowerShell
Microsoft’s New PowerShell Crescendo Tool Facilitates Native Command-Line Wraps
Mar 21, 2022 | Rabia Noureen
Most popular on petri