Skip to content

Commit cf59f62

Browse files
New-DbaAgentSchedule: Add new parameter FrequencyText (#9847)
1 parent f3b7fff commit cf59f62

File tree

2 files changed

+98
-0
lines changed

2 files changed

+98
-0
lines changed

public/New-DbaAgentSchedule.ps1

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,23 @@ function New-DbaAgentSchedule {
8888
8989
FrequencyRecurrenceFactor is used only if FrequencyType is "Weekly", "Monthly" or "MonthlyRelative".
9090
91+
.PARAMETER FrequencyText
92+
Describe common frequencies as a text. Sample text:
93+
94+
Every minute
95+
Every 5 minutes
96+
Every 10 minutes starting at 00:02:30
97+
Every hour
98+
Every 2 hours
99+
Every 4 hours starting at 02:00:00
100+
Every day at 05:00:00
101+
Every sunday at 02:00:00
102+
103+
This is the used regex: every(\s+(?<interval>\d+))?\s+(?<unit>minute|hour|day|sunday|monday|tuesday|wednesday|thursday|friday|saturday)s?(\s+starting)?(\s+at\s+(?<start>\d\d:\d\d:\d\d))?
104+
105+
If parameter Schedule is not provided, the FrequencyText will be used as the name of the schedule.
106+
Parameter Force will be set to $true.
107+
91108
.PARAMETER StartDate
92109
The earliest date this schedule can execute jobs, formatted as yyyyMMdd (e.g., "20240315" for March 15, 2024).
93110
Use this to delay schedule activation until a future date or to document when recurring maintenance should begin.
@@ -157,6 +174,12 @@ function New-DbaAgentSchedule {
157174
PS C:\> New-DbaAgentSchedule -SqlInstance sstad-pc -Schedule RunWeekly -FrequencyType Weekly -FrequencyInterval Sunday -StartTime 010000 -Force
158175
159176
Create a schedule that will run jobs once a week on Sunday @ 1:00AM
177+
178+
.EXAMPLE
179+
PS C:\> New-DbaAgentSchedule -SqlInstance sstad-pc -FrequencyText 'Every sunday at 02:00:00'
180+
181+
Create a schedule with the name "Every sunday at 02:00:00" that will run jobs once a week on Sunday @ 2:00AM
182+
160183
#>
161184
[CmdletBinding(SupportsShouldProcess, ConfirmImpact = "Low")]
162185
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseOutputTypeCorrectly", "", Justification = "PSSA Rule Ignored by BOH")]
@@ -176,6 +199,7 @@ function New-DbaAgentSchedule {
176199
[ValidateSet('Unused', 'First', 'Second', 'Third', 'Fourth', 'Last')]
177200
[object]$FrequencyRelativeInterval,
178201
[int]$FrequencyRecurrenceFactor,
202+
[string]$FrequencyText,
179203
[string]$StartDate,
180204
[string]$EndDate,
181205
[string]$StartTime,
@@ -188,6 +212,42 @@ function New-DbaAgentSchedule {
188212
begin {
189213
if ($Force) { $ConfirmPreference = 'none' }
190214

215+
if ($FrequencyText) {
216+
if ($FrequencyText -match 'every(\s+(?<interval>\d+))?\s+(?<unit>minute|hour|day|sunday|monday|tuesday|wednesday|thursday|friday|saturday)s?(\s+starting)?(\s+at\s+(?<start>\d\d:\d\d:\d\d))?') {
217+
$textInterval = $Matches['interval']
218+
$textUnit = $Matches['unit']
219+
$textStart = $Matches['start']
220+
221+
if (-not $textInterval) {
222+
$textInterval = 1
223+
}
224+
225+
if ($textUnit -in 'minute', 'hour', 'day') {
226+
$FrequencyType = 'Daily'
227+
if ($textUnit -in 'minute', 'hour') {
228+
$FrequencySubdayType = $textUnit
229+
$FrequencySubdayInterval = $textInterval
230+
}
231+
} else {
232+
$FrequencyType = 'Weekly'
233+
$FrequencyInterval = $textUnit
234+
}
235+
236+
if ($textStart) {
237+
$StartTime = $textStart.Replace(':', '')
238+
}
239+
240+
if (-not $Schedule) {
241+
$Schedule = $FrequencyText
242+
}
243+
244+
$Force = $true
245+
} else {
246+
Stop-Function -Message "FrequencyText can not be parsed."
247+
return
248+
}
249+
}
250+
191251
if ($FrequencyType -eq "Daily" -and -not $FrequencyInterval) {
192252
$FrequencyInterval = 1
193253
}

tests/New-DbaAgentSchedule.Tests.ps1

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Describe $CommandName -Tag UnitTests {
2222
"FrequencySubdayInterval",
2323
"FrequencyRelativeInterval",
2424
"FrequencyRecurrenceFactor",
25+
"FrequencyText",
2526
"StartDate",
2627
"EndDate",
2728
"StartTime",
@@ -267,4 +268,41 @@ Describe $CommandName -Tag IntegrationTests {
267268
}
268269
}
269270
}
271+
272+
Context "Should create schedules based on frequency texts" {
273+
It "Should create a schedule for: Every minute" {
274+
$results = New-DbaAgentSchedule -SqlInstance $TestConfig.instance2 -FrequencyText 'Every minute'
275+
$results.Name | Should -Be 'Every minute'
276+
$results.Description | Should -BeLike 'Occurs every day every 1 minute(s) between 12:00:00 AM and 11:59:59 PM*'
277+
$results | Remove-DbaAgentSchedule
278+
}
279+
280+
It "Should create a schedule for: Every 10 minutes starting at 00:02:30" {
281+
$results = New-DbaAgentSchedule -SqlInstance $TestConfig.instance2 -FrequencyText 'Every 10 minutes starting at 00:02:30'
282+
$results.Name | Should -Be 'Every 10 minutes starting at 00:02:30'
283+
$results.Description | Should -BeLike 'Occurs every day every 10 minute(s) between 12:02:30 AM and 11:59:59 PM*'
284+
$results | Remove-DbaAgentSchedule
285+
}
286+
287+
It "Should create a schedule for: Every 2 hours" {
288+
$results = New-DbaAgentSchedule -SqlInstance $TestConfig.instance2 -FrequencyText 'Every 2 hours'
289+
$results.Name | Should -Be 'Every 2 hours'
290+
$results.Description | Should -BeLike 'Occurs every day every 2 hour(s) between 12:00:00 AM and 11:59:59 PM*'
291+
$results | Remove-DbaAgentSchedule
292+
}
293+
294+
It "Should create a schedule for: Every day at 05:00:00" {
295+
$results = New-DbaAgentSchedule -SqlInstance $TestConfig.instance2 -FrequencyText 'Every day at 05:00:00'
296+
$results.Name | Should -Be 'Every day at 05:00:00'
297+
$results.Description | Should -BeLike 'Occurs every day at 5:00:00 AM*'
298+
$results | Remove-DbaAgentSchedule
299+
}
300+
301+
It "Should create a schedule for: Every sunday at 02:00:00" {
302+
$results = New-DbaAgentSchedule -SqlInstance $TestConfig.instance2 -FrequencyText 'Every sunday at 02:00:00'
303+
$results.Name | Should -Be 'Every sunday at 02:00:00'
304+
$results.Description | Should -BeLike 'Occurs every week on Sunday at 2:00:00 AM*'
305+
$results | Remove-DbaAgentSchedule
306+
}
307+
}
270308
}

0 commit comments

Comments
 (0)