Skip to content
Menu
The Lonely Administrator
  • PowerShell Tips & Tricks
  • Books & Training
  • Essential PowerShell Learning Resources
  • Privacy Policy
  • About Me
The Lonely Administrator

Converting Timespans to Repetition Patterns

Posted on May 26, 2015

Over on Petri.com, I've recently published a followup article about creating daily or weekly scheduled PowerShell jobs that support a repetition interval. The short answer is to use the Scheduled Tasks cmdlets. In the Petri article I talk about needing to use a special string format for the timespan. It is documented on MSDN.

Manage and Report Active Directory, Exchange and Microsoft 365 with
ManageEngine ADManager Plus - Download Free Trial

Exclusive offer on ADManager Plus for US and UK regions. Claim now!

It isn't especially difficult to work it out, but I thought it would be handy to have a PowerShell function to convert a timespan to this format.

Function ConvertTo-RepetitionPattern {
<#
.Synopsis
Convert a timespan to an XML repetition pattern
.Description
When creating scheduled tasks, repetition duration and intervals must be set using a special XML format.

P<days>DT<hours>H<minutes>M<seconds>S

The minimum time allowed is 1 minute and the maximum is 31 days.
.Example
PS C:\> new-timespan -Minutes 10 | ConvertTo-RepetitionPattern
PT10M
.Example
PS C:\> ConvertTo-RepetitionPattern "0:1:10:0"
PT1H10M
.Link
https://msdn.microsoft.com/en-us/library/windows/desktop/aa382117%28v=vs.85%29.aspx#properties
.Link
New-Timespan
#>
[cmdletbinding()]
Param(
[Parameter(Position=0,Mandatory,HelpMessage = "Enter a timespan",
ValueFromPipeline)]
[ValidateScript({$_.totalminutes -ge 1 -AND $_.totaldays -le 31})]
[timespan]$Timespan
)

Begin {
    Write-Verbose "Starting $($MyInvocation.Mycommand)"
    [string]$pattern = "P"  
} #begin

Process {
    Write-Verbose "Converting $Timespan"
    if ($Timespan.Days -gt 0) {
        Write-Verbose "$($timespan.days) Days"
        $Pattern+= "{0}D" -f $Timespan.Days
    }
    #insert T separater
    $Pattern+="T"

    if ($Timespan.Hours -gt 0) {
    Write-Verbose "$($timespan.hours) Hours"
        $Pattern+= "{0}H" -f $Timespan.hours
    }
    if ($Timespan.Minutes -gt 0) {
    Write-Verbose "$($timespan.Minutes) Minutes"
        $Pattern+= "{0}M" -f $Timespan.Minutes
    }
    if ($Timespan.Seconds -gt 0) {
        Write-Verbose "$($timespan.seconds) Seconds"
        $Pattern+= "{0}S" -f $Timespan.Seconds
    }

    #write the result to the pipeline
    $pattern
}
End {
    Write-Verbose "Ending $($MyInvocation.Mycommand)"
} #end

} #end function

Technically, the pattern will support any timespan but since the pattern used with scheduled tasks must be between 1 minute and 31 days, I added that as a validation parameter. The function simply builds the final string based on the number of date time elements, i.e. hours, minutes and seconds.

Hopefully this function makes it easier to automate the scheduled job and task process.


Behind the PowerShell Pipeline

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on Mastodon (Opens in new window) Mastodon
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to share on Pocket (Opens in new window) Pocket
  • Click to share on Reddit (Opens in new window) Reddit
  • Click to print (Opens in new window) Print
  • Click to email a link to a friend (Opens in new window) Email

Like this:

Like Loading...

Related

reports

Powered by Buttondown.

Join me on Mastodon

The PowerShell Practice Primer
Learn PowerShell in a Month of Lunches Fourth edition


Get More PowerShell Books

Other Online Content

github



PluralSightAuthor

Active Directory ADSI Automation Backup Books CIM CLI conferences console Friday Fun FridayFun Function functions Get-WMIObject GitHub hashtable HTML Hyper-V Iron Scripter ISE Measure-Object module modules MrRoboto new-object objects Out-Gridview Pipeline PowerShell PowerShell ISE Profile prompt Registry Regular Expressions remoting SAPIEN ScriptBlock Scripting Techmentor Training VBScript WMI WPF Write-Host xml

©2025 The Lonely Administrator | Powered by SuperbThemes!
%d