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