-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathInvoke-MissingUpdateInstallation.ps1
More file actions
304 lines (212 loc) · 11.6 KB
/
Invoke-MissingUpdateInstallation.ps1
File metadata and controls
304 lines (212 loc) · 11.6 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
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
Function Invoke-MissingUpdateInstallation {
<#
.SYNOPSIS
This cmdlet is used to install missing Windows and 3rd Party Application updates that are approved in SCCMs Software Center. You can specify a retry count for updates that fail the first time or two. This uses CIM connections to accomplish the task
.DESCRIPTION
This cmdlet can be run locally on a machine to look for and install any approved SCCM server approved updates. This allows you to attempt multiple times to install an update since the first attempt will seomtimes fail
.PARAMETER ComputerName
Define remote computer(s) you wish to install missing updates on
.PARAMETER KBs
Define the specific KBs you wish to install
.PARAMETER Credential
Define the credentials that should be used to remotely connect to devices using CIM sessions with WinRM
.PARAMETER Seconds
Define the number of seconds to wait in between retry attempts
.PARAMETER RetryCount
Define the number of times to retry and installation before giving up on it
.PARAMETER UseSSL
Build CIM Sessions using WinRM over HTTPS
.EXAMPLE
Invoke-MissingUpdateInstallation
# This example attempts to install any missing SCCM approved updates on the local device. It retries failed updates twice with 90 second intervals bewteen attempts
.EXAMPLE
Invoke-MissingUpdateInstallation -ComputerName DC01.domain.com,FS01.domain.com,DHCP.domain.com -UseSSL -Credential $LiveCred -Seconds 45 -RetryCount 1
# This example installs missing updates on the 3 defined remote machines over a WinRM over HTTPS CIM session. If an attempt fails it will wait 45 seconds and retry once more before moving on to the next update check
.INPUTS
System.String ComputerName
.OUTPUTS
PSCustomObject
.NOTES
Author: Robert H. Osborne
Alias: tobor
Contact: rosborne@osbornepro.com
.LINK
https://github.com/tobor88
https://github.com/OsbornePro
https://www.powershellgallery.com/profiles/tobor
https://osbornepro.com
https://writeups.osbornepro.com
https://btpssecpack.osbornepro.com
https://www.powershellgallery.com/profiles/tobor
https://www.hackthebox.eu/profile/52286
https://www.linkedin.com/in/roberthosborne/
https://www.credly.com/users/roberthosborne/badges
#>
[CmdletBinding(DefaultParameterSetName="Local")]
param(
[Parameter(
ParameterSetName="Remote",
Position=0,
Mandatory=$True,
ValueFromPipeline=$True,
ValueFromPipelineByPropertyName=$True)] # End Parameter
[Parameter(
ParameterSetName="Local",
Position=0,
Mandatory=$False,
ValueFromPipeline=$True,
ValueFromPipelineByPropertyName=$True)] # End Parameter
[String[]]$ComputerName = "$env:COMPUTERNAME.$((Get-CimInstance -ClassName Win32_ComputerSystem).Domain)",
[Parameter(
Mandatory=$False,
Position=1,
ValueFromPipeline=$False
)] # End Parameter
[String[]]$KBs,
[ValidateNotNull()]
[System.Management.Automation.PSCredential]
[System.Management.Automation.Credential()]
[Parameter(ParameterSetName="Remote")]
$Credential = [System.Management.Automation.PSCredential]::Empty,
[Parameter(
Mandatory=$False,
ValueFromPipeline=$False
)] # End Parameter
[Int32]$Seconds = 90,
[Parameter(
Mandatory=$False,
ValueFromPipeline=$False
)] # End Parameter
[Int32]$RetryCount = 3,
[Parameter(
ParameterSetName="Remote",
Mandatory=$False)] # End Parameter
[Switch][Bool]$UseSSL
) # End param
BEGIN {
$Return = @()
$NotReachable = @()
$ConfirmSSL = $False
If ($UseSSL.IsPresent) {
$ConfirmSSL = $True
} # End If
} PROCESS {
If ($PSCmdlet.ParameterSetName -eq "Remote") {
Write-Verbose "Builindg CIM Sessions for $ComputerName"
$CIMSession = New-CimSession -ComputerName $ComputerName -Credential $Credential -SessionOption (New-CIMSessionOption -SkipCACheck -SkipCNCheck -SkipRevocationCheck -UseSsl:$ConfirmSSL) -ErrorAction SilentlyContinue
$CIMConnections = (Get-CimSession).ComputerName
ForEach ($C in $ComputerName) {
If ($C -notin $CIMConnections) {
$NotReachable += $C
} # End If
} # End ForEach
} # End If
Write-Verbose "Getting the missing updates from SCCM clients"
If ($CIMSession) {
Write-Output "[*] Getting missing updates on $ComputerName"
[CimInstance[]]$MissingUpdates = Get-CimInstance -CimSession $CIMSession -NameSpace "Root\CCM\ClientSDK" -ClassName "CCM_SoftwareUpdate" -Filter "ComplianceState=0" -ErrorAction Stop
} Else {
Write-Output "[*] Getting Missing updates on $env:COMPUTERNAME"
[CimInstance[]]$MissingUpdates = Get-CimInstance -NameSpace "Root\CCM\ClientSDK" -ClassName "CCM_SoftwareUpdate" -Filter "ComplianceState=0"
} # End If Else
If ($Null -eq $MissingUpdates) {
Write-Output "[*] No missing updates found."
} Else {
If ($PSBoundParameters.ContainsKey('KBs')) {
[CimInstance[]]$MissingUpdates = $MissingUpdates | Where-Object -FilterScript { $_.ArticleID -in $KBs.Replace("KB","") }
} # End If
Write-Output "[*] Installing the below missing updates: `n$($MissingUpdates | Select-Object -ExpandProperty ArticleID -Unique)"
If ($CIMSession) {
Invoke-CimMethod -CimSession $CIMSession -Namespace "Root\CCM\ClientSDK" -ClassName "CCM_SoftwareUpdatesManager" -MethodName InstallUpdates -Arguments @{ CCMUpdates=[CimInstance[]]$MissingUpdates} -ErrorAction SilentlyContinue | Out-Null
} Else {
Invoke-CimMethod -Namespace "Root\CCM\ClientSDK" -ClassName "CCM_SoftwareUpdatesManager" -MethodName InstallUpdates -Arguments @{ CCMUpdates=[CimInstance[]]$MissingUpdates} -ErrorAction SilentlyContinue | Out-Null
} # End If Else
ForEach ($C in $ComputerName) {
ForEach ($MissingUpdate in $MissingUpdates) {
$State = $MissingUpdate | Select-Object -ExpandProperty EvaluationState
Switch ($State) {
'0' { $JobState = "ciJobStateNone" }
'1' { $JobState = "ciJobStateAvailable" }
'2' { $JobState = "ciJobStateSubmitted" }
'3' { $JobState = "ciJobStateDetecting" }
'4' { $JobState = "ciJobStatePreDownload" }
'5' { $JobState = "ciJobStateDownloading" }
'6' { $JobState = "ciJobStateWaitInstall" }
'7' { $JobState = "ciJobStateInstalling" }
'8' { $JobState = "ciJobStatePendingSoftReboot" }
'9' { $JobState = "ciJobStatePendingHardReboot" }
'10' { $JobState = "ciJobStateWaitReboot" }
'11' { $JobState = "ciJobStateVerifying" }
'12' { $JobState = "ciJobStateInstallComplete" }
'13' { $JobState = "ciJobStateError" }
'14' { $JobState = "ciJobStateWaitServiceWindow" }
'15' { $JobState = "ciJobStateWaitUserLogon" }
'16' { $JobState = "ciJobStateWaitUserLogoff" }
'17' { $JobState = "ciJobStateWaitJobUserLogon" }
'18' { $JobState = "ciJobStateWaitUserReconnect" }
'19' { $JobState = "ciJobStatePendingUserLogoff" }
'20' { $JobState = "ciJobStatePendingUpdate" }
'21' { $JobState = "ciJobStateWaitingRetry" }
'22' { $JobState = "ciJobStateWaitPresModeOff" }
'23' { $JobState = "ciJobStateWaitForOrchestration" }
} # End Switch
$Return += New-Object -TypeName PSCustomObject -Property @{
JobState=$JobState;
ComputerName=$C;
Update=$MissingUpdate.Name;
PercentComplete=$MissingUpdate.PercentComplete;
ArticleId=$MissingUpdate.ArticleID;
ComplianceState=$MissingUpdate.ComplianceState;
Deadline=$MissingUpdate.Deadline;
UpdateID=$MissingUpdate.UpdateID;
} # End New-Object -Property
} # End ForEach
} # End ForEach
$NotReachable | ForEach-Object {
$Return += New-Object -TypeName PSCustomObject -Property @{
JobState="CIM Session could not be created";
ComputerName=$_;
Update="CIM Session could not be created";
PercentComplete="CIM Session could not be created";
ArticleId="CIM Session could not be created";
ComplianceState="CIM Session could not be created";
Deadline="CIM Session could not be created";
UpdateID="CIM Session could not be created";
} # End New-Object -Property
} # End ForEach
Write-Output "[*] Waiting $Seconds seconds for updates to reach an in progress related status"
Start-Sleep -Seconds $Seconds
$Result = $True
$Counter = 0
While ($Result -eq $True -or $Counter -ne $RetryCount) {
If ($CIMSession) {
$CCMUpdate = Get-CimInstance -CimSession $CIMSession -Namespace "Root\CCM\ClientSDK" -ClassName "CCM_SoftwareUpdate" -ErrorAction SilentlyContinue
} Else {
$CCMUpdate = Get-CimInstance -Namespace "Root\CCM\ClientSDK" -ClassName "CCM_SoftwareUpdate"
} # End If Else
[Array]$UniqueStatus = $CCMUpdate | Sort-Object -Property EvalutationState -Unique
If ([Array]$UniqueStatus.EvaluationState -Contains 13 -or $UniqueStatus -Contains 21) {
$RetryUpdate = $CCMUpdate | Where-Object -FilterScript { $_.EvalutationState -eq 13 -or $_.EvaluationState -eq 21 }
$RetryCIM = $CIMSession | Where-Object -FilterScript { $_.ComputerName -in $RetryUpdate.ComputerName }
Write-Output "[*] Retrying one last time the installation attempt of the missing updates"
Invoke-CimMethod -ComputerName $RetryCIM -Namespace "Root\CCM\ClientSDK" -ClassName "CCM_SoftwareUpdatesManager" -MethodName InstallUpdates -Arguments @{ CCMUpdates=[CimInstance[]]$MissingUpdates} -ErrorAction SilentlyContinue | Out-Null
Write-Output "[*] Waiting $Seconds Seconds for update to start"
Start-Sleep -Seconds $Seconds
} # End If
$Result = If (@($CCMUpdate | Where-Object -FilterScript { $_.EvaluationState -eq 2 -or $_.EvaluationState -eq 3 -or $_.EvaluationState -eq 4 -or $_.EvaluationState -eq 5 -or $_.EvaluationState -eq 6 -or $_.EvaluationState -eq 7 -or $_.EvaluationState -eq 11 }).length -ne 0) {
$True
} Else {
$False
} # End If Else
Start-Sleep -Seconds 5
$Counter++
} # End While
If ($CIMSession) {
Write-Verbose "Closing CIM Sessions"
Remove-CimSession -CimSession $CIMSession -Confirm:$False -ErrorAction SilentlyContinue
} # End If
} # End If Else
} END {
Return $Return
} # End B P E
} # End Function Invoke-MissingUpdateInstallation