-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRollingRestart.psm1
249 lines (188 loc) · 6.81 KB
/
RollingRestart.psm1
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
#Requires -Version 3 -Module ActiveDirectory
$ScriptPath = Split-Path $Script:MyInvocation.MyCommand.Path
$Global:Dependencies = "$ScriptPath\Dependencies.txt"
$Global:Transcript = "$ScriptPath\Transcript.txt"
function Get-RRServer {
<#
.SYNOPSIS
.DESCRIPTION
.PARAMETER ComputerName
.EXAMPLE
#>
[CmdletBinding()]
param(
[Parameter(Mandatory, ParameterSetName='ByGroup')]
[ValidateScript({ Get-ADGroup $_ })]
[String]$Group,
[Parameter(Mandatory, ParameterSetName='ByComputer')]
[String[]]$ComputerName,
[ValidateScript({ Test-Path $_ })]
[String]$DependenciesFile = $Global:Dependencies
)
PROCESS {
if ($PSCmdlet.ParameterSetName -eq 'ByGroup') {
$ComputerName = (Get-ADGroupMember $Group).Name
}
$FileLines = Get-Content $DependenciesFile
$IndependentArray = @()
$DependentArray = @()
Write-Verbose "COMPARE EACH COMPUTER FROM PATCH GROUP WITH DEPENDENCIES FILE"
foreach ($Computer in $ComputerName) {
if ($FileLines.Split(',') -contains $Computer) {
Write-Verbose "Add DependentArray : $Computer"
$DependentArray += $Computer
} else {
Write-Verbose "Add IndependentArray: $Computer"
$IndependentArray += $Computer
}
}
#OUTPUT INDEPENDENT COMPUTERNAME AS STRING
foreach ($Computer in $IndependentArray) {
[PSCustomObject]@{
"ComputerName"=$Computer;
"Type"="Independent";
}
}
Write-Verbose "COMPARE EACH DEPENDENCIEFILE ROW WITH DEPENDENT ARRAY"
#For every row within file
for ($r=0; $r -lt $FileLines.Length; $r++) {
$RowArray = @()
#For every column within row
for ($c=0; $c -lt $FileLines[$r].Split(',').Length; $c++) {
$FileComputer = $FileLines[$r].Split(',')[$c]
if ($DependentArray -contains $FileComputer) {
Write-Verbose "$r,$c; Add RowArray: $FileComputer"
$RowArray += $FileComputer
} else {
Write-Verbose "$r,$c; Skip Server : $FileComputer"
}
} #for Column
#OUTPUT DEPENDENT COMPUTERNAME AS ARRAY
if ($RowArray) {
[PSCustomObject]@{
"ComputerName"=$RowArray;
"Type"="Dependent";
}
}
} #for Row
} #PROCESS
}
workflow Restart-RRIndependent {
[CmdletBinding()]
param(
[Parameter(Mandatory)]
[String[]]$ComputerName,
[int]$Sleep = 60,
[Switch]$Apply
)
foreach -Parallel ($Computer in $ComputerName) {
try {
if ($Apply) {
Write-Verbose "Dependent,$Computer,Restarting,$(Get-Date)"
Restart-Computer -PSComputerName $Computer -Force
} else {
Write-Verbose "Dependent,$Computer,TestRestart,$(Get-Date)"
}
#Added for additional delay
Start-Sleep -Seconds $Sleep
} catch {
Write-Warning "$Computer,$_"
}
}
}
workflow Restart-RRDependent {
<# multidimensional array#>
[CmdletBinding(ConfirmImpact = 'High')]
param(
#[Parameter(Mandatory,ValueFromPipeline)]
[Parameter(Mandatory,ValueFromPipelineByPropertyName)]
[string[]]$ComputerName,
[int]$Sleep = 60,
[switch]$Apply
)
foreach ($Computer in $ComputerName) {
try {
if ($Apply) {
Write-Verbose "Dependent,$Computer,Restarting,$(Get-Date)"
Restart-Computer -PSComputerName $Computer -Force -Wait
Write-Verbose "Dependent,$Computer,Restarted,$(Get-Date)"
} else {
Write-Verbose "Dependent,$Computer,TestRestart,$(Get-Date)"
}
#Added for additional delay
Start-Sleep -Seconds $Sleep
} catch {
Write-Warning "$Computer,$_"
}
}
}
function Get-RRGroup {
[CmdletBinding()]
param(
[DateTime]$RunDate = (Get-Date)
)
Write-Verbose "Run Date: $RunDate"
#First day of the month
[DateTime]$FirstTuesday = $RunDate.ToString().Replace($RunDate.Day,'1')
#First Tuesday of the month
while ($FirstTuesday.DayOfWeek -ine 'Tuesday') {
$FirstTuesday=$FirstTuesday.AddDays(1)
}
Write-Verbose "First Tuesday: $FirstTuesday"
$PatchTuesday = $FirstTuesday.AddDays(7)
Write-Verbose "Patch Tuesday: $PatchTuesday"
#Format Date
[DateTime]$PT = Get-Date -f d ($PatchTuesday)
[DateTime]$Run = Get-Date -f d ($RunDate)
#Group based on RunDate relative to Patch Tuesday
Switch ($Run) {
($PT.AddDays(7)) {$Group = "SCCM Server Patch A"}
($PT.AddDays(14)) {$Group = "SCCM Server Patch B"}
($PT.AddDays(-7)) {$Group = "SCCM Server Patch C"}
default {$Group = $null; Write-Verbose "No Server Group"}
}
$Group
}
function Use-RollingRestart {
[CmdletBinding(SupportsShouldProcess,ConfirmImpact = 'High')]
param()
Start-Transcript -Path $Global:Transcript
#Get group based on date
$Group = Get-RRGroup -Verbose
#If no group, do not continue
if (!$Group) {Write-Warning "No Patch Maintenance Today!"; Stop-Transcript; Return}
#Get Object (ComputerName, Type) from Group and Dependencies File
$ds = Get-RRServer -Group $Group
#Log in transcript in case there is an existing job
if (Get-Job) {
Write-Warning 'EXISTING JOBS'
Get-Job
Get-Job | Receive-Job
Get-Job | Remove-Job -Force
}else {
Write-Host 'NO EXISTING JOBS'
}
#Extra for error handling
if (Get-Job) {Write-Warning 'DID NOT REMOVE JOBS'; Get-Job}
if ($PSCmdlet.ShouldProcess()) {
#PROCESS INDEPENDENT COMPUTERS VIA PARALLEL WORKFLOW
Restart-RRIndependent -ComputerName ($ds | ? Type -eq Independent).ComputerName -AsJob -JobName "Independent" | Out-Null
#PROCESS DEPENDENT COMPUTER ARRAY VIA SEQUENTIAL WORKFLOW
$ds | ? Type -eq Dependent | % { Restart-RRDependent -ComputerName $_.ComputerName -AsJob -JobName "$($_.ComputerName[0])" | Out-Null }
}
$i = 0
#Required to give jobs time to execute
while (Get-Job) {
#Break out of loop if run for 1h (30 x 120s)
$i += 1; if ($i -eq 30) {break}
Write-Host $i
Get-Job -State Completed | Receive-Job
#Workflow Jobs are not removed by Receive-Job; -AutoRemoveJob didn't work either
Get-Job -HasMoreData $false | Remove-Job
#Log any jobs that may have errors
Get-Job | select Name,State | ft -HideTableHeaders
Start-Sleep -Seconds 120
}
if ($i -eq 30) {Write-Warning 'TIMOUT'; Get-Job}
Stop-Transcript
}