-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathNew-CMScheduleStartTime.ps1
100 lines (97 loc) · 4.64 KB
/
New-CMScheduleStartTime.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
Function New-CMScheduleStartTime {
[CmdletBinding()]
<#
.SYNOPSIS
Recreate a CMSchedule object with a new start time
.DESCRIPTION
Natively, the CMSchedule objects do not allow you to write to the StartTime property. This makes it
difficult to adjust the start time of an existing maintenance window. This function can be used to
'recreate' a CMSchedule based on the input schedule, with a new start time.
.PARAMETER CMSchedule
An array of CMSchedule objects
.PARAMETER StartTime
The desired new start time for the schedule
.EXAMPLE
CCM:\> $Sched = Get-CMMaintenanceWindow -CollectionName 'test'
$Schedobject = Convert-CMSchedule -ScheduleString $Sched.ServiceWindowSchedules
New-CMScheduleStartTime -CMSchedule $Schedobject -StartTime $Schedobject.StartTime.AddDays(5)
.NOTES
FileName: New-CMScheduleStartTime.ps1
Author: Cody Mathis
Contact: @CodyMathis123
Created: 2020-02-26
Updated: 2020-02-26
#>
Param(
[Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)]
[Alias('Schedules')]
[Microsoft.ConfigurationManagement.ManagementProvider.WqlQueryEngine.WqlResultObjectBase[]]$CMSchedule,
[Parameter(Mandatory = $true)]
[datetime]$StartTime
)
begin {
#region create our splat for the new schedule, start time is the same for all
$NewSchedSplat = @{
Start = $StartTime
}
#endregion create our splat for the new schedule, start time is the same for all
}
process {
foreach ($Schedule in $CMSchedule) {
#region determine new end time based off new start time, and existing durations
$NewEndTime = $StartTime.AddDays($Schedule.DayDuration).AddHours($Schedule.HourDuration).AddMinutes($Schedule.MinuteDuration)
#endregion determine new end time based off new start time, and existing durations
#region define the paramters that are the same for all 'new' schedules
$NewSchedSplat['End'] = $NewEndTime
$NewSchedSplat['IsUTC'] = $Schedule.IsGMT
#endregion define the paramters that are the same for all 'new' schedules
try {
#region based on recur type, we will add parameters to our $NewSchedSplat
Switch ($Schedule.SmsProviderObjectPath) {
SMS_ST_NonRecurring {
$NewSchedSplat['Nonrecurring'] = $true
}
SMS_ST_RecurInterval {
if ($Schedule.MinuteSpan -ne 0) {
$Span = 'Minutes'
$Interval = $Schedule.MinuteSpan
}
elseif ($Schedule.HourSpan -ne 0) {
$Span = 'Hours'
$Interval = $Schedule.HourSpan
}
elseif ($Schedule.DaySpan -ne 0) {
$Span = 'Days'
$Interval = $Schedule.DaySpan
}
$NewSchedSplat['RecurInterval'] = $Span
$NewSchedSplat['RecurCount'] = $Interval
}
SMS_ST_RecurWeekly {
$NewSchedSplat['DayOfWeek'] = [DayOfWeek]($Day - $Schedule.Day)
$NewSchedSplat['RecurCount'] = $Schedule.ForNumberOfWeeks
}
SMS_ST_RecurMonthlyByWeekday {
$NewSchedSplat['DayOfWeek'] = [DayOfWeek]($Day - $Schedule.Day)
$NewSchedSplat['WeekOrder'] = $Schedule.WeekOrder
$NewSchedSplat['RecurCount'] = $Schedule.ForNumberOfMonths
}
SMS_ST_RecurMonthlyByDate {
$NewSchedSplat['DayOfMonth'] = $Schedule.MonthDay
$NewSchedSplat['RecurCount'] = $Schedule.ForNumberOfMonths
}
Default {
Write-Error "Parsing Schedule String resulted in invalid type of $RecurType" -ErrorAction Stop
}
}
#endregion based on recur type, we will add parameters to our $NewSchedSplat
#region return our new CMSchedule
New-CMSchedule @NewSchedSplat
#endregion return our new CMSchedule
}
Catch {
$_.Exception.Message
}
}
}
}