-
Notifications
You must be signed in to change notification settings - Fork 14
/
Remove-WindowsUpdate.ps1
262 lines (167 loc) · 8.64 KB
/
Remove-WindowsUpdate.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
<#
.SYNOPSIS
This cmdlet is for uninstalling a Windows Update. This can remove multiple hot fixes
and it can remove hot fixes from an array of remote computers.
.DESCRIPTION
Remove-WindowsUpdate is a cmdlet that is used to remove a speficied Windows Update or Updates
from a local computer or a remote host or hosts. A list of computer names can be piped to this
function by property name.
.PARAMETER HotFixID
Specifies the hotfix IDs that this cmdlet gets.
.PARAMETER ComputerName
Specifies a remote computer. The default is the local computer.
Type the NetBIOS name, an Internet Protocol (IP) address, or a fully qualified domain name (FQDN) of a remote computer.
.PARAMETER Restart
Specifies whether or not the remote computer should be restarted after the patch is removed.
.EXAMPLE
Remove-WindowsUpdate -HotFixID "4556799"
This examples uninstalls 4556799 from the local computer if it is installed.
.EXAMPLE
Remove-WindowsUpdate "KB4556799"
This examples also uninstalls HotFix KB4556799 from the local computer.
.EXAMPLE
Remove-WindowsUpdate -HotFixID "KB4556799" -ComputerName 10.10.10.120 -Restart
This examples uninstalls HotFix KB4556799 from a remote computer at 10.10.10.120 and if a restart is needed allows it to restart.
.EXAMPLE
Remove-WindowsUpdate "KB4556799" 10.10.10.120
This examples also uninstalls HotFix KB4556799 from a remote computer at 10.10.10.120.
.NOTES
Author: Robert H. Osborne
Alias: tobor
Contact: [email protected]
.LINK
https://osbornepro.com
https://writeups.osbornepro.com
https://btpssecpack.osbornepro.com
https://github.com/tobor88
https://gitlab.com/tobor88
https://www.powershellgallery.com/profiles/tobor
https://www.linkedin.com/in/roberthosborne/
https://www.credly.com/users/roberthosborne/badges
https://www.hackthebox.eu/profile/52286
.INPUTS
System.String
You can pipe computer names to this cmdlet..
In Windows PowerShell 2.0, the ComputerName parameter takes input from the pipeline only by property name. In
Windows PowerShell 3.0, the ComputerName parameter takes input from the pipeline by value.
.OUTPUTS
None, System.Management.Automation.RemotingJob
This cmdlet returns a job object, if you specify the AsJob parameter. Otherwise, it does not generate anyoutput.
#>
Function Remove-WindowsUpdate {
[CmdletBinding()]
param(
[Parameter(
Mandatory=$True,
Position=0,
ValueFromPipeline=$False,
HelpMessage="Enter the Windows Update KB number(s) you wish to uninstall. Separate multiple values with a comma.`nExample: KB4556799','KB4556798' (4556799 is also acceptable) `n")] # End Paramater
[String[]]$HotFixID,
[Parameter(
Mandatory=$False,
Position=1,
ValueFromPipeline=$True,
ValueFromPipelineByPropertyName=$True,
HelpMessage="Enter the name or names of the remote compute you wish to uninstall. Separate multiple values with a comma. `nExample: 'Comp1.domain.com','Comp2','10.10.10.123'`n")] # End Paramater
[ValidateNotNullOrEmpty()]
[String[]]$ComputerName,
[Parameter(
Mandatory=$False)]
[switch][bool]$Restart
) # End param
BEGIN {
If ($ComputerName) {
For ($i = 0; $i -lt $ComputerName.Count ; $i++) {
ForEach ($Computer in $ComputerName) {
Write-Verbose "[*] Testing specified $Computer is reachable"
If (Test-Connection -ComputerName $Computer -Quiet -ErrorAction Inquire) {
Write-Verbose "[*] $Computer is reachable"
Try {
If ($Null -eq $Cred) {
$Cred = Get-Credential -Message "Administrator Credentials are required to execute commands on remote hosts" -Username ($env:USERNAME + "@" + ((Get-WmiObject Win32_ComputerSystem).Domain))
} # End If
New-Variable -Name "Session$i" -Value (New-PsSession -ComputerName $Computer -Credential $Cred -Name $Computer -EnableNetworkAccess -Port 5986 -UseSSL)
} # End Try
Catch {
Write-Verbose "[*] Skipping certificate validation checks to create an encrypted session with the remote host."
New-Variable -Name "Session$i" -Value (New-PsSession -ComputerName $Computer -Credential $Cred -EnableNetworkAccess -Port 5986 -UseSSL -SessionOption (New-PsSessionOption -SkipCACheck -SkipCNCheck -SkipRevocationCheck))
} # End Catch
} # End If
} # End ForEach
} # End For
} # End If
} # End BEGIN
PROCESS
{
If ($ComputerName) {
For ($n = 0; $n -lt $ComputerName.Count; $n++) {
ForEach ($C in $ComputerName) {
Write-Verbose "[*] Starting connection to $C"
Invoke-Command -Session (Get-Variable -Name "Session$n").Value -ArgumentList $HotFixID -ScriptBlock {
param([array]$HotFixID)
Write-Output "[*] Getting list of installed patches"
$PatchList = Get-CimInstance -ClassName "Win32_QuickFixEngineering" -Namespace "root\cimv2"
ForEach ($HotFix in $HotFixID) {
$Patch = $PatchList | Where-Object { $_.HotFixID -like "$HotFix" }
Write-Output "[*] $Patch will be removed from $env:COMPUTERNAME"
If (!($Patch)) {
Write-Output "[!] The Windows Update KB number you defined is not installed on $env:COMPUTERNAME. Below is a table of installed patches: "
Remove-Variable -Name "Patch"
$PatchList
} # End If
Else {
Write-Output "[*] $HotFix is installed on $env:COMPUTERNAME, continuing uninstallation"
$KBNumber = $Patch.HotfixId.Replace("KB", "") | Out-String
If ($Restart.IsPresent) {
Write-Output "[*] Restart switch parameter is defined. You will be prompted to restart."
cmd /c wusa /uninstall /kb:$KBNumber /promptrestart /log
} # End If
Else {
cmd /c wusa /uninstall /kb:$KBNumber /norestart /log
} # End Else
While (@(Get-Process wusa -ErrorAction SilentlyContinue).Count -ne 0) {
Start-Sleep -Seconds 10
Write-Host "Waiting for update removal to finish. Please wait..."
} # End While
} # End Else
} # End ForEach
} # End Invoke-Command
Write-Verbose "[*] Finished removing updates from $C"
} # End ForEach
} # End For
} # End If
Else {
Write-Verbose "[*] Getting list of installed patches on $env:COMPUTERNAME"
$PatchList = Get-CimInstance -ClassName "Win32_QuickFixEngineering" -Namespace "root\cimv2"
ForEach ($HotFix in $HotFixID) {
$Patch = $PatchList | Where-Object { $_.HotFixID -like "$HotFix" }
If (!($Patch)) {
Write-Output "[!] The Windows Update KB number you defined is not installed on $env:COMPUTERNAME. Below is a table of installed patches: "
Remove-Variable -Name "Patch"
$PatchList
} # End If
Else {
$KBNumber = $Patch.HotfixId.Replace("KB", "") | Out-String
If ($Restart.IsPresent) {
Write-Output "[*] Restart switch parameter is defined. You will be prompted to restart."
cmd /c wusa /uninstall /kb:$KBNumber /norestart /log
} # End If
Else {
cmd /c wusa /uninstall /kb:$KBNumber /norestart /log
} # End Else
While (@(Get-Process wusa -ErrorAction SilentlyContinue).Count -ne 0) {
Start-Sleep -Seconds 10
Write-Output "[*] Waiting for update removal to finish. Please wait..."
} # End While
Write-Output "[*] Update removal has completed"
} # End Else
} # End ForEach
} # End Else
} # End PROCESS
END {
If (Get-PsSession) {
Write-Verbose "[*] Closing connection to remote computers."
Remove-PsSession *
} # End If
} # End END
} # End Function Remove-WindowsUpdate