-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBlock BSOD patches for Printing.ps1
499 lines (444 loc) · 23.4 KB
/
Block BSOD patches for Printing.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
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
Function Hide-WUUpdate
{
[OutputType('PSWindowsUpdate.WUList')]
[CmdletBinding(
SupportsShouldProcess=$True,
ConfirmImpact="High"
)]
Param
(
#Pre search criteria
[ValidateSet("Driver", "Software")]
[String]$UpdateType = "",
[String[]]$UpdateID,
[Int]$RevisionNumber,
[String[]]$CategoryIDs,
[Switch]$IsInstalled,
[Switch]$IsHidden,
[Switch]$IsNotHidden,
[String]$Criteria,
[Switch]$ShowSearchCriteria,
#Post search criteria
[String[]]$Category="",
[String[]]$KBArticleID,
[String]$Title,
[String[]]$NotCategory="",
[String[]]$NotKBArticleID,
[String]$NotTitle,
[Alias("Silent")]
[Switch]$IgnoreUserInput,
[Switch]$IgnoreRebootRequired,
#Connection options
[String]$ServiceID,
[Switch]$WindowsUpdate,
[Switch]$MicrosoftUpdate,
[Switch]$HideStatus = $true,
#Mode options
[Switch]$Debuger,
[parameter(ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true)]
[String[]]$ComputerName
)
Begin
{
If($PSBoundParameters['Debuger'])
{
$DebugPreference = "Continue"
} #End If $PSBoundParameters['Debuger']
$User = [Security.Principal.WindowsIdentity]::GetCurrent()
$Role = (New-Object Security.Principal.WindowsPrincipal $user).IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)
if(!$Role)
{
Write-Warning "To perform some operations you must run an elevated Windows PowerShell console."
} #End If !$Role
}
Process
{
Write-Debug "STAGE 0: Prepare environment"
######################################
# Start STAGE 0: Prepare environment #
######################################
Write-Debug "Check if ComputerName in set"
If($ComputerName -eq $null)
{
Write-Debug "Set ComputerName to localhost"
[String[]]$ComputerName = $env:COMPUTERNAME
} #End If $ComputerName -eq $null
####################################
# End STAGE 0: Prepare environment #
####################################
$UpdateCollection = @()
Foreach($Computer in $ComputerName)
{
If(Test-Connection -ComputerName $Computer -Quiet)
{
Write-Debug "STAGE 1: Get updates list"
###################################
# Start STAGE 1: Get updates list #
###################################
If($Computer -eq $env:COMPUTERNAME)
{
Write-Debug "Create Microsoft.Update.ServiceManager object"
$objServiceManager = New-Object -ComObject "Microsoft.Update.ServiceManager" #Support local instance only
Write-Debug "Create Microsoft.Update.Session object for $Computer"
$objSession = New-Object -ComObject "Microsoft.Update.Session" #Support local instance only
} #End If $Computer -eq $env:COMPUTERNAME
Else
{
Write-Debug "Create Microsoft.Update.Session object for $Computer"
$objSession = [activator]::CreateInstance([type]::GetTypeFromProgID("Microsoft.Update.Session",$Computer))
} #End Else $Computer -eq $env:COMPUTERNAME
Write-Debug "Create Microsoft.Update.Session.Searcher object for $Computer"
$objSearcher = $objSession.CreateUpdateSearcher()
If($WindowsUpdate)
{
Write-Debug "Set source of updates to Windows Update"
$objSearcher.ServerSelection = 2
$serviceName = "Windows Update"
} #End If $WindowsUpdate
ElseIf($MicrosoftUpdate)
{
Write-Debug "Set source of updates to Microsoft Update"
$serviceName = $null
Foreach ($objService in $objServiceManager.Services)
{
If($objService.Name -eq "Microsoft Update")
{
$objSearcher.ServerSelection = 3
$objSearcher.ServiceID = $objService.ServiceID
$serviceName = $objService.Name
Break
}#End If $objService.Name -eq "Microsoft Update"
}#End ForEach $objService in $objServiceManager.Services
If(-not $serviceName)
{
Write-Warning "Can't find registered service Microsoft Update. Use Get-WUServiceManager to get registered service."
Return
}#Enf If -not $serviceName
} #End Else $WindowsUpdate If $MicrosoftUpdate
ElseIf($Computer -eq $env:COMPUTERNAME) #Support local instance only
{
Foreach ($objService in $objServiceManager.Services)
{
If($ServiceID)
{
If($objService.ServiceID -eq $ServiceID)
{
$objSearcher.ServiceID = $ServiceID
$objSearcher.ServerSelection = 3
$serviceName = $objService.Name
Break
} #End If $objService.ServiceID -eq $ServiceID
} #End If $ServiceID
Else
{
If($objService.IsDefaultAUService -eq $True)
{
$serviceName = $objService.Name
Break
} #End If $objService.IsDefaultAUService -eq $True
} #End Else $ServiceID
} #End Foreach $objService in $objServiceManager.Services
} #End Else $MicrosoftUpdate If $Computer -eq $env:COMPUTERNAME
ElseIf($ServiceID)
{
$objSearcher.ServiceID = $ServiceID
$objSearcher.ServerSelection = 3
$serviceName = $ServiceID
}
Else #End Else $Computer -eq $env:COMPUTERNAME If $ServiceID
{
$serviceName = "default (for $Computer) Windows Update"
} #End Else $ServiceID
Write-Debug "Set source of updates to $serviceName"
Write-Verbose "Connecting to $serviceName server. Please wait..."
Try
{
$search = ""
If($Criteria)
{
$search = $Criteria
} #End If $Criteria
Else
{
If($IsInstalled)
{
$search = "IsInstalled = 1"
Write-Debug "Set pre search criteria: IsInstalled = 1"
} #End If $IsInstalled
Else
{
$search = "IsInstalled = 0"
Write-Debug "Set pre search criteria: IsInstalled = 0"
} #End Else $IsInstalled
If($UpdateType -ne "")
{
Write-Debug "Set pre search criteria: Type = $UpdateType"
$search += " and Type = '$UpdateType'"
} #End If $UpdateType -ne ""
If($UpdateID)
{
Write-Debug "Set pre search criteria: UpdateID = '$([string]::join(", ", $UpdateID))'"
$tmp = $search
$search = ""
$LoopCount = 0
Foreach($ID in $UpdateID)
{
If($LoopCount -gt 0)
{
$search += " or "
} #End If $LoopCount -gt 0
If($RevisionNumber)
{
Write-Debug "Set pre search criteria: RevisionNumber = '$RevisionNumber'"
$search += "($tmp and UpdateID = '$ID' and RevisionNumber = $RevisionNumber)"
} #End If $RevisionNumber
Else
{
$search += "($tmp and UpdateID = '$ID')"
} #End Else $RevisionNumber
$LoopCount++
} #End Foreach $ID in $UpdateID
} #End If $UpdateID
If($CategoryIDs)
{
Write-Debug "Set pre search criteria: CategoryIDs = '$([string]::join(", ", $CategoryIDs))'"
$tmp = $search
$search = ""
$LoopCount =0
Foreach($ID in $CategoryIDs)
{
If($LoopCount -gt 0)
{
$search += " or "
} #End If $LoopCount -gt 0
$search += "($tmp and CategoryIDs contains '$ID')"
$LoopCount++
} #End Foreach $ID in $CategoryIDs
} #End If $CategoryIDs
If($IsNotHidden)
{
Write-Debug "Set pre search criteria: IsHidden = 0"
$search += " and IsHidden = 0"
} #End If $IsNotHidden
ElseIf($IsHidden)
{
Write-Debug "Set pre search criteria: IsHidden = 1"
$search += " and IsHidden = 1"
} #End ElseIf $IsHidden
#Don't know why every update have RebootRequired=false which is not always true
If($IgnoreRebootRequired)
{
Write-Debug "Set pre search criteria: RebootRequired = 0"
$search += " and RebootRequired = 0"
} #End If $IgnoreRebootRequired
} #End Else $Criteria
Write-Debug "Search criteria is: $search"
If($ShowSearchCriteria)
{
Write-Output $search
} #End If $ShowSearchCriteria
$objResults = $objSearcher.Search($search)
} #End Try
Catch
{
If($_ -match "HRESULT: 0x80072EE2")
{
Write-Warning "Probably you don't have connection to Windows Update server"
} #End If $_ -match "HRESULT: 0x80072EE2"
Return
} #End Catch
$NumberOfUpdate = 1
$PreFoundUpdatesToDownload = $objResults.Updates.count
Write-Verbose "Found [$PreFoundUpdatesToDownload] Updates in pre search criteria"
If($PreFoundUpdatesToDownload -eq 0)
{
Continue
} #End If $PreFoundUpdatesToDownload -eq 0
Foreach($Update in $objResults.Updates)
{
$UpdateAccess = $true
Write-Progress -Activity "Post search updates for $Computer" -Status "[$NumberOfUpdate/$PreFoundUpdatesToDownload] $($Update.Title) $size" -PercentComplete ([int]($NumberOfUpdate/$PreFoundUpdatesToDownload * 100))
Write-Debug "Set post search criteria: $($Update.Title)"
If($Category -ne "")
{
$UpdateCategories = $Update.Categories | Select-Object Name
Write-Debug "Set post search criteria: Categories = '$([string]::join(", ", $Category))'"
Foreach($Cat in $Category)
{
If(!($UpdateCategories -match $Cat))
{
Write-Debug "UpdateAccess: false"
$UpdateAccess = $false
} #End If !($UpdateCategories -match $Cat)
Else
{
$UpdateAccess = $true
Break
} #End Else !($UpdateCategories -match $Cat)
} #End Foreach $Cat in $Category
} #End If $Category -ne ""
If($NotCategory -ne "" -and $UpdateAccess -eq $true)
{
$UpdateCategories = $Update.Categories | Select-Object Name
Write-Debug "Set post search criteria: NotCategories = '$([string]::join(", ", $NotCategory))'"
Foreach($Cat in $NotCategory)
{
If($UpdateCategories -match $Cat)
{
Write-Debug "UpdateAccess: false"
$UpdateAccess = $false
Break
} #End If $UpdateCategories -match $Cat
} #End Foreach $Cat in $NotCategory
} #End If $NotCategory -ne "" -and $UpdateAccess -eq $true
If($KBArticleID -ne $null -and $UpdateAccess -eq $true)
{
Write-Debug "Set post search criteria: KBArticleIDs = '$([string]::join(", ", $KBArticleID))'"
If(!($KBArticleID -match $Update.KBArticleIDs -and "" -ne $Update.KBArticleIDs))
{
Write-Debug "UpdateAccess: false"
$UpdateAccess = $false
} #End If !($KBArticleID -match $Update.KBArticleIDs)
} #End If $KBArticleID -ne $null -and $UpdateAccess -eq $true
If($NotKBArticleID -ne $null -and $UpdateAccess -eq $true)
{
Write-Debug "Set post search criteria: NotKBArticleIDs = '$([string]::join(", ", $NotKBArticleID))'"
If($NotKBArticleID -match $Update.KBArticleIDs -and "" -ne $Update.KBArticleIDs)
{
Write-Debug "UpdateAccess: false"
$UpdateAccess = $false
} #End If$NotKBArticleID -match $Update.KBArticleIDs -and "" -ne $Update.KBArticleIDs
} #End If $NotKBArticleID -ne $null -and $UpdateAccess -eq $true
If($Title -and $UpdateAccess -eq $true)
{
Write-Debug "Set post search criteria: Title = '$Title'"
If($Update.Title -notmatch $Title)
{
Write-Debug "UpdateAccess: false"
$UpdateAccess = $false
} #End If $Update.Title -notmatch $Title
} #End If $Title -and $UpdateAccess -eq $true
If($NotTitle -and $UpdateAccess -eq $true)
{
Write-Debug "Set post search criteria: NotTitle = '$NotTitle'"
If($Update.Title -match $NotTitle)
{
Write-Debug "UpdateAccess: false"
$UpdateAccess = $false
} #End If $Update.Title -notmatch $NotTitle
} #End If $NotTitle -and $UpdateAccess -eq $true
If($IgnoreUserInput -and $UpdateAccess -eq $true)
{
Write-Debug "Set post search criteria: CanRequestUserInput"
If($Update.InstallationBehavior.CanRequestUserInput -eq $true)
{
Write-Debug "UpdateAccess: false"
$UpdateAccess = $false
} #End If $Update.InstallationBehavior.CanRequestUserInput -eq $true
} #End If $IgnoreUserInput -and $UpdateAccess -eq $true
If($IgnoreRebootRequired -and $UpdateAccess -eq $true)
{
Write-Debug "Set post search criteria: RebootBehavior"
If($Update.InstallationBehavior.RebootBehavior -ne 0)
{
Write-Debug "UpdateAccess: false"
$UpdateAccess = $false
} #End If $Update.InstallationBehavior.RebootBehavior -ne 0
} #End If $IgnoreRebootRequired -and $UpdateAccess -eq $true
If($UpdateAccess -eq $true)
{
Write-Debug "Convert size"
Switch($Update.MaxDownloadSize)
{
{[System.Math]::Round($_/1KB,0) -lt 1024} { $size = [String]([System.Math]::Round($_/1KB,0))+" KB"; break }
{[System.Math]::Round($_/1MB,0) -lt 1024} { $size = [String]([System.Math]::Round($_/1MB,0))+" MB"; break }
{[System.Math]::Round($_/1GB,0) -lt 1024} { $size = [String]([System.Math]::Round($_/1GB,0))+" GB"; break }
{[System.Math]::Round($_/1TB,0) -lt 1024} { $size = [String]([System.Math]::Round($_/1TB,0))+" TB"; break }
default { $size = $_+"B" }
} #End Switch
Write-Debug "Convert KBArticleIDs"
If($Update.KBArticleIDs -ne "")
{
$KB = "KB"+$Update.KBArticleIDs
} #End If $Update.KBArticleIDs -ne ""
Else
{
$KB = ""
} #End Else $Update.KBArticleIDs -ne ""
if($Update.IsHidden -ne $HideStatus)
{
if($HideStatus)
{
$StatusName = "Hide"
} #$HideStatus
else
{
$StatusName = "Unhide"
} #Else $HideStatus
If($pscmdlet.ShouldProcess($Computer,"$StatusName $($Update.Title)?"))
{
Try
{
$Update.IsHidden = $HideStatus
}
Catch
{
Write-Warning "You haven't privileges to make this. Try start an eleated Windows PowerShell console."
}
} #$pscmdlet.ShouldProcess($Computer,"Hide $($Update.Title)?")
} #End $Update.IsHidden -ne $HideStatus
$Status = ""
If($Update.IsDownloaded) {$Status += "D"} else {$status += "-"}
If($Update.IsInstalled) {$Status += "I"} else {$status += "-"}
If($Update.IsMandatory) {$Status += "M"} else {$status += "-"}
If($Update.IsHidden) {$Status += "H"} else {$status += "-"}
If($Update.IsUninstallable) {$Status += "U"} else {$status += "-"}
If($Update.IsBeta) {$Status += "B"} else {$status += "-"}
Add-Member -InputObject $Update -MemberType NoteProperty -Name ComputerName -Value $Computer
Add-Member -InputObject $Update -MemberType NoteProperty -Name KB -Value $KB
Add-Member -InputObject $Update -MemberType NoteProperty -Name Size -Value $size
Add-Member -InputObject $Update -MemberType NoteProperty -Name Status -Value $Status
$Update.PSTypeNames.Clear()
$Update.PSTypeNames.Add('PSWindowsUpdate.WUList')
$UpdateCollection += $Update
} #End If $UpdateAccess -eq $true
$NumberOfUpdate++
} #End Foreach $Update in $objResults.Updates
Write-Progress -Activity "Post search updates for $Computer" -Status "Completed" -Completed
$FoundUpdatesToDownload = $UpdateCollection.count
Write-Verbose "Found [$FoundUpdatesToDownload] Updates in post search criteria"
#################################
# End STAGE 1: Get updates list #
#################################
} #End If Test-Connection -ComputerName $Computer -Quiet
} #End Foreach $Computer in $ComputerName
Return $UpdateCollection
} #End Process
End{}
}
Hide-WUUpdate -KBArticleID KB5000802 -Confirm:$false
Hide-WUUpdate -KBArticleID KB5000822 -Confirm:$false
Hide-WUUpdate -KBArticleID KB5000808 -Confirm:$false
Hide-WUUpdate -KBArticleID KB5000809 -Confirm:$false
$WindowsBuild = (Get-Item "HKLM:SOFTWARE\Microsoft\Windows NT\CurrentVersion").GetValue('ReleaseID')
IF ($WindowsBuild -eq 2004)
{Write-Host "Removing 5000802"
wusa /uninstall /kb:5000802 /quiet }
Else
{}
IF ($WindowsBuild -eq 1909)
{Write-Host "Removing 5000808"
wusa /uninstall /kb:5000808 /quiet }
Else
{}
IF ($WindowsBuild -eq 1809)
{Write-Host "Removing 5000822"
wusa /uninstall /kb:5000822 /quiet}
Else
{}
IF ($WindowsBuild -eq 1803)
{Write-Host "Removing 5000809"
wusa /uninstall /kb:5000809 /quiet}
Else
{}