-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSend-UpdateNotification2.ps1
More file actions
162 lines (96 loc) · 6.3 KB
/
Copy pathSend-UpdateNotification2.ps1
File metadata and controls
162 lines (96 loc) · 6.3 KB
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# Used to send out e-mail notifications about servers that are patching
# Find Patch Tuesday (2nd Tuesday of the month)
[DateTime]$date = Get-Date -Format "MM-01-yyyy"
switch ($date.DayOfWeek){
"Sunday" {$patchTuesday = 9}
"Monday" {$patchTuesday = 8}
"Tuesday" {$patchTuesday = 7}
"Wednesday" {$patchTuesday = 13}
"Thursday" {$patchTuesday = 12}
"Friday" {$patchTuesday = 11}
"Saturday" {$patchTuesday = 10}
}
# Import CM PS modules
import-module (Join-Path $(Split-Path $ENV:SMS_ADMIN_UI_PATH) ConfigurationManager.psd1)
# Set site code
set-location ABC:
CD ABC:
# Patching Server Pilot – Auto Patch & Auto-Restart / Nightly maintenance window [10PM to 5AM]
# Set the maint window (should match maint window in SCCM)
$patchStart = (Get-Date $date).AddDays($patchTuesday).AddHours(22)
$patchEnd = (Get-Date $patchStart).AddHours(7)
# Figure out what time it is
$currentDate = Get-Date
$startDT = $PatchStart.ToString("s")
$endDT = $PatchEnd.ToString("s")
$SendNotificationStartTime=$PatchStart.AddDays(-1).AddHours(-14)
$SendNotificationEndTime=$SendNotificationStartTime.AddMinutes(45)
# If it's 24 hours before patching time, set $Sendmail to True, otherwise set to False
if(([DateTime]($currentDate) -ge $SendNotificationStartTime) -and ([DateTime]($currentDate) -le $SendNotificationEndTime )){$SendEmail = "True"}else{$SendEmail = "False"}
if ($SendEmail -eq "True") {
# Dump collection members from SCCM collection into text (CSV) format
$serverpilot = Get-CMCollectionMember -CollectionName "_Server Pilot - Auto-patch & Auto-restart" | Select Name | Sort-Object -Property Name | ConvertTo-Csv -NoTypeInformation
# Add break returns <br> for each computer name
ForEach ($_ in $serverpilot) {
$serverpilotbr += "$_ <br>"
}
# Send out e-mail.
Write-host "Sending out e-mail..."
Send-MailMessage -From 'vm-acme-01@acme.com' -To 'patches@acme.com' -Subject 'Server Pilot Patching' -Body "The following servers will be
patched and restarted between $PatchStart and $PatchEnd . Please ignore NAGIOS/PRTG alerts during the maintenance window for these servers. <br> $serverpilotbr" -SmtpServer '127.0.0.1' -BodyAsHtml
$SendEmail = "False"
}
# Patching All Test/Dev & VM-XXX/DP-XXX Odds/DC Odds – Auto-Patch & Auto-Restart / +4 days after Patch Tuesday which will be the 2nd or 3rd weekend of the month [Saturday 5PM to Sunday 5PM] – Auto-patch and Auto-restart
$patchStart = (Get-Date $date).AddDays($patchTuesday + 4).AddHours(22)
$patchEnd = (Get-Date $patchStart).AddHours(7)
$currentDate = Get-Date
$startDT = $PatchStart.ToString("s")
$endDT = $PatchEnd.ToString("s")
$SendNotificationStartTime=$PatchStart.AddDays(-1).AddHours(-14)
$SendNotificationEndTime=$SendNotificationStartTime.AddMinutes(45)
if(([DateTime]($currentDate) -ge $SendNotificationStartTime) -and ([DateTime]($currentDate) -le $SendNotificationEndTime )){$SendEmail = "True"}else{$SendEmail = "False"}
if ($SendEmail -eq "True"){
$serverplus4 = Get-CMCollectionMember -CollectionName "_All Test/Dev & VM-XXX/DP-XXX Odds/DC Odds - Auto-patch & Auto-restart" | Select Name | Sort-Object -Property Name | ConvertTo-Csv -NoTypeInformation
ForEach ($_ in $serverplus4) {
$serverplus4br += "$_ <br>"
}
Send-MailMessage -From 'vm-acme-01@acme.com' -To 'patches@acme.com' -Subject 'Server Weekend Patching +4 days after Patch Tuesday' -Body "The following servers will be
patched and restarted between $PatchStart and $PatchEnd . Please ignore NAGIOS/PRTG alerts during the maintenance window for these servers. <br> $serverplus4br" -SmtpServer '127.0.0.1' -BodyAsHtml
$SendEmail = "False"
}
# _DC/CM (All) & VM-XXX/DP-XXX Evens - Auto-patch and Auto-restart
$patchStart = (Get-Date $date).AddDays($patchTuesday + 11).AddHours(22)
$patchEnd = (Get-Date $patchStart).AddHours(7)
$currentDate = Get-Date
$startDT = $PatchStart.ToString("s")
$endDT = $PatchEnd.ToString("s")
$SendNotificationStartTime=$PatchStart.AddDays(-1).AddHours(-14)
$SendNotificationEndTime=$SendNotificationStartTime.AddMinutes(45)
if(([DateTime]($currentDate) -ge $SendNotificationStartTime) -and ([DateTime]($currentDate) -le $SendNotificationEndTime )){$SendEmail = "True"}else{$SendEmail = "False"}
if ($SendEmail -eq "True") {
$serverplus11 = Get-CMCollectionMember -CollectionName "_DC/CM (All) & VM-XXX/DP-XXX Evens - Auto-patch and Auto-restart" | Select Name | Sort-Object -Property Name | ConvertTo-Csv -NoTypeInformation
ForEach ($_ in $serverplus11) {
$serverplus11br += "$_ <br>"
}
Send-MailMessage -From 'vm-acme-01@acme.com' -To 'patches@acme.com' -Subject 'Server Weekend Patching +11 days after Patch Tuesday' -Body "The following servers will be
patched and restarted between $PatchStart and $PatchEnd . Please ignore NAGIOS/PRTG alerts during the maintenance window for these servers. <br> $serverplus11br" -SmtpServer '127.0.0.1' -BodyAsHtml
$SendEmail = "False"
}
# _Production Servers – Auto-Patch & Auto-Restart
$patchStart = (Get-Date $date).AddDays($patchTuesday + 18).AddHours(22)
$patchEnd = (Get-Date $patchStart).AddHours(7)
$currentDate = Get-Date
$startDT = $PatchStart.ToString("s")
$endDT = $PatchEnd.ToString("s")
$SendNotificationStartTime=$PatchStart.AddDays(-2).AddHours(-14)
$SendNotificationEndTime=$SendNotificationStartTime.AddMinutes(45)
if(([DateTime]($currentDate) -ge $SendNotificationStartTime) -and ([DateTime]($currentDate) -le $SendNotificationEndTime )){$SendEmail = "True"}else{$SendEmail = "False"}
if ($SendEmail -eq "True"){
$serverplus18 = Get-CMCollectionMember -CollectionName "_Production Servers – Auto-Patch & Auto-Restart" | Select Name | Sort-Object -Property Name | ConvertTo-Csv -NoTypeInformation
ForEach ($_ in $serverplus18) {
$serverplus18br += "$_ <br>"
}
Send-MailMessage -From 'vm-acme-01@acme.com' -To 'patches@acme.com' -Subject 'Server Weekend Patching +18 days after Patch Tuesday' -Body "The following servers will be
patched and restarted between $PatchStart and $PatchEnd . Please ignore NAGIOS/PRTG alerts during the maintenance window for these servers. <br> $serverplus18br" -SmtpServer '127.0.0.1' -BodyAsHtml
$SendEmail = "False"
}