PowerShell

Creating Repeating PowerShell Scheduled Jobs

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)
New-JobTrigger information in Windows PowerShell. (Image Credit: Jeff Hicks)

Unfortunately, this only works if you are creating a one-time job. You can create a daily or weekly job to repeat in the Task Scheduler, so why can’t you use PowerShell? Here’s one way that might work.

If you look at a job trigger object, you’ll see properties for repetition settings.

Sponsored Content

What is “Inside Microsoft Teams”?

“Inside Microsoft Teams” is a webcast series, now in Season 4 for IT pros hosted by Microsoft Product Manager, Stephen Rose. Stephen & his guests comprised of customers, partners, and real-world experts share best practices of planning, deploying, adopting, managing, and securing Teams. You can watch any episode at your convenience, find resources, blogs, reviews of accessories certified for Teams, bonus clips, and information regarding upcoming live broadcasts. Our next episode, “Polaris Inc., and Microsoft Teams- Reinventing how we work and play” will be airing on Oct. 28th from 10-11am PST.

Repetition settings for New-JobTrigger in Windows PowerShell. (Image Credit: Jeff Hicks)
Repetition settings for New-JobTrigger in Windows PowerShell. (Image Credit: Jeff Hicks)

You might even think you can set these properties and create a new job.
https://petri.com/creating-repeating-powershell-scheduled-jobs$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

Sadly, when you look at the job in Task Scheduler, the repetition settings are not configured.

Repetition settings in Task Scheduler. (Image Credit: Jeff Hicks)
Repetition settings in Task Scheduler. (Image Credit: Jeff Hicks)

I was hoping that PowerShell would simply apply the trigger object, but that doesn’t seem to be the case. The only time repetition settings appear to be configured is with a one-time job. So how do we solve this? We can use a separate, but related set of cmdlets.

Windows 8 and Windows Server 2012 brought us another module called ScheduledTasks. You can use these cmdlets to manage all scheduled tasks. Because a PowerShell scheduled job is just another scheduled task, we can use the task cmdlets to modify it. Let’s grab the job I just created using Get-ScheduledTask.
https://petri.com/creating-repeating-powershell-scheduled-jobs$t = get-scheduledtask -TaskName "Weekly Rep Test A"
This object has a triggers property that includes a repetition pattern.

All we need to do is set the Duration and Interval properties. However, the values are not simple timespans. The values are defined using a special string format: P<days>DT<hours>H<minutes>M<seconds>S. For example, an interval of four hours would be PT4H.

For my purposes, I want my test scheduled job to run once a minute for an hour.
https://petri.com/creating-repeating-powershell-scheduled-jobs$t.Triggers.repetition.Duration = 'PT60M'
$t.Triggers.repetition.Interval = 'PT01M'

That’s all there is to it other than updating the scheduled task.
https://petri.com/creating-repeating-powershell-scheduled-jobs$t | Set-ScheduledTask
When I refresh the task in Task Scheduler, I now have repetition values.

Updated repetition values in Task Scheduler. (Image Credit: Jeff Hicks)
Updated repetition values in Task Scheduler. (Image Credit: Jeff Hicks)

Because I jumped around a bit, here’s an example from start to finish.

​
$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.

Updating repetition settings so our PowerShell command runs every minute for 10 minutes. (Image Credit: Jeff Hicks)
Updating repetition settings so our PowerShell command runs every minute for 10 minutes. (Image Credit: Jeff Hicks)
Interestingly, the scheduled job cmdlets don't recognize these settings.
The scheduled job cmdlets do not recognize our repetition settings. (Image Credit: Jeff Hicks)
The scheduled job cmdlets do not recognize our repetition settings. (Image Credit: Jeff Hicks)
But these settings are present when viewed from task scheduler.
Our settings are visible when viewed from the Task Scheduler. (Image Credit: Jeff Hicks)
Our settings are visible when viewed from the Task Scheduler. (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.

Related Topics:

BECOME A PETRI MEMBER:

Don't have a login but want to join the conversation? Sign up for a Petri Account

Register
Comments (0)

Leave a Reply

External Sharing and Guest User Access in Microsoft 365 and Teams

This eBook will dive into policy considerations you need to make when creating and managing guest user access to your Teams network, as well as the different layers of guest access and the common challenges that accompany a more complicated Microsoft 365 infrastructure.

You will learn:

  • Who should be allowed to be invited as a guest?
  • What type of guests should be able to access files in SharePoint and OneDrive?
  • How should guests be offboarded?
  • How should you determine who has access to sensitive information in your environment?

Sponsored by:

 
Live Webinar: Active Directory Security: What Needs Immediate Priority!Live on Tuesday, October 12th at 1 PM ET

Attacks on Active Directory are at an all-time high. Companies that are not taking heed are being punished, both monetarily and with loss of production.

In this webinar, you will learn:

  • How to prioritize vulnerability management
  • What attackers are leveraging to breach organizations
  • Where Active Directory security needs immediate attention
  • Overall strategy to secure your environment and keep it secured

Sponsored by: