-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathSave-KbUpdate.ps1
More file actions
323 lines (271 loc) · 14.1 KB
/
Save-KbUpdate.ps1
File metadata and controls
323 lines (271 loc) · 14.1 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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
function Save-KbUpdate {
<#
.SYNOPSIS
Downloads patches from Microsoft
.DESCRIPTION
Downloads patches from Microsoft
.PARAMETER Pattern
Any pattern. Can be the KB name, number or even MSRC numbrer. For example, KB4057119, 4057119, or MS15-101.
.PARAMETER Path
The directory to save the file.
.PARAMETER FilePath
The exact file name to save to, otherwise, it uses the name given by the webserver
.PARAMETER Architecture
Can be x64, x86, ia64, or ARM.
.PARAMETER OperatingSystem
Specify one or more operating systems. Tab complete to see what's available. If anything is missing, please file an issue.
.PARAMETER Product
Specify one or more products (SharePoint, SQL Server, etc). Tab complete to see what's available. If anything is missing, please file an issue.
.PARAMETER Language
Specify one or more Language. Tab complete to see what's available.
.PARAMETER Latest
Filters out any patches that have been superseded by other patches in the batch
.PARAMETER Source
Search source. By default, Database is searched first, then if no matches are found, it tries finding it on the web if a an internet connection is detected.
.PARAMETER InputObject
Enables piping from Get-KbUpdate
.PARAMETER AllowClobber
Overwrite file if it exsits
.PARAMETER EnableException
By default, when something goes wrong we try to catch it, interpret it and give you a friendly warning message.
This avoids overwhelming you with "sea of red" exceptions, but is inconvenient because it basically disables advanced scripting.
Using this switch turns this "nice by default" feature off and enables you to catch exceptions with your own try/catch.
.PARAMETER Link
When link is specified only the links in the array are processed and downloaded to the system.
.NOTES
Tags: Update
Author: Chrissy LeMaire (@cl), netnerds.net
Copyright: (c) licensed under MIT
License: MIT https://opensource.org/licenses/MIT
.EXAMPLE
PS C:\> Save-KbUpdate -Pattern KB4057119
Downloads KB4057119 to the current directory. This works for SQL Server or any other KB.
.EXAMPLE
PS C:\> Save-KbUpdate -Pattern MS15-101
Downloads KBs related to MSRC MS15-101 to the current directory.
.EXAMPLE
PS C:\> Get-KbUpdate -Pattern 3118347 -Simple -Architecture x64 | Out-GridView -Passthru | Save-KbUpdate
Downloads the selected files from KB4057119 to the current directory.
.EXAMPLE
PS C:\> Save-KbUpdate -Pattern KB4057119, 4057114 -Architecture x64 -Path C:\temp
Downloads KB4057119 and the x64 version of KB4057114 to C:\temp.
.EXAMPLE
PS C:\> Save-KbUpdate -Pattern KB4057114 -Path C:\temp
Downloads all versions of KB4057114 and the x86 version of KB4057114 to C:\temp.
.EXAMPLE
PS C:\> Save-KBUpdate -Link $downloadLink -Path C:\temp
Downloads all files from $downloadLink to C:\temp
#>
[CmdletBinding(DefaultParameterSetName = 'default')]
param(
[Parameter(ValueFromPipelineByPropertyName, Mandatory, ParameterSetName = 'link')]
[string[]]$Link,
[Parameter(ValueFromPipelineByPropertyName, Position = 0)]
[Alias("UpdateId", "Id", "KBUpdate", "HotfixId", "Name")]
[string[]]$Pattern,
[string]$Path = ".",
[string]$FilePath,
[string[]]$Architecture,
[string[]]$OperatingSystem,
[string[]]$Product,
[string]$Language,
[parameter(ValueFromPipeline)]
[pscustomobject[]]$InputObject,
[switch]$Latest,
[switch]$AllowClobber,
[ValidateSet("Wsus", "Web", "Database")]
[string[]]$Source = (Get-PSFConfigValue -FullName kbupdate.app.source),
[switch]$EnableException
)
begin {
$jobs = $inputobjects = $uniquelinks = @()
$count = 0
}
process {
if ($InputObject) {
$inputobjects += $InputObject
}
if ($Link) {
$uniquelinks += $Link
}
}
end {
if ($uniquelinks) {
$uniquelinks = $uniquelinks | Select-Object -Unique
}
switch ($PSCmdlet.ParameterSetName) {
'link' {
Write-PSFMessage -Level Verbose -Message "Processing link parameter set"
$uniquelinks | Foreach-Object {
$hyperlinklol = $PSItem
$fileName = Split-Path $hyperlinklol -Leaf
if ($FilePath) {
$filename = $FilePath
}
$count++
if ($count -eq 300) {
$count = 1
}
$file = Join-Path -Path $Path -ChildPath $filename
if ((Test-Path -Path $file) -and -not $AllowClobber) {
Get-ChildItem -Path $file
continue
}
if (-not $filePath) {
$FilePath = $file
}
Write-PSFMessage -Level Verbose -Message "Link: $PSItem"
Write-PSFMessage -Level Verbose -Message "FilePath: $FilePath"
Write-PSFMessage -Level Verbose -Message "File: $file"
# just show any progress since piping won't allow calculation of the total
$percentcomplete = $(($count / 300) * 100)
if ($percentcomplete -lt 0 -or $percentcomplete -gt 100) {
$percentcomplete = 0
}
$progressparms = @{
Activity = "Queuing up downloads"
Status = "Adding files to download queue"
PercentComplete = $percentcomplete
}
Write-Progress @progressparms
if ((Get-Command Start-BitsTransfer -ErrorAction Ignore)) {
try {
if ((Get-BitsTransfer | Where-Object Description -match kbupdate).FileList.RemoteName -notcontains $hyperlinklol) {
Write-PSFMessage -Level Verbose -Message "Adding $filename to download queue"
$jobs += Start-BitsTransfer -Asynchronous -Source $hyperlinklol -Destination $Path -ErrorAction Stop -Description kbupdate
}
} catch {
Write-PSFMessage -Level Verbose -Message "Going to use uri: $hyperlinklol"
Write-Progress -Activity "Downloading $FilePath" -Id 1
Invoke-TlsWebRequest -OutFile $file -Uri $hyperlinklol
Write-Progress -Activity "Downloading $FilePath" -Id 1 -Completed
}
} else {
try {
Write-PSFMessage -Level Verbose -Message "Transfer failed. Trying again."
# IWR is crazy slow for large downloads
Write-Progress -Activity "Downloading $FilePath" -Id 1
Invoke-TlsWebRequest -OutFile $file -Uri $hyperlinklol
Write-Progress -Activity "Downloading $FilePath" -Id 1 -Completed
} catch {
Stop-PSFFunction -EnableException:$EnableException -Message "Failure" -ErrorRecord $_ -Continue
}
}
if ((Test-Path -Path $file)) {
Get-ChildItem -Path $file
}
}
}
default {
Write-PSFMessage -Level Verbose -Message "Processing default parameter set"
if ($Pattern.Count -gt 1 -and $PSBoundParameters.FilePath) {
Stop-PSFFunction -EnableException:$EnableException -Message "You can only specify one KB when using FilePath"
return
}
if (-not $PSBoundParameters.InputObject -and -not $PSBoundParameters.Pattern) {
Stop-PSFFunction -EnableException:$EnableException -Message "You must specify a KB name or pipe in results from Get-KbUpdate"
return
}
if (-not $PSBoundParameters.InputObject -and ($PSBoundParameters.OperatingSystem -or $PSBoundParameters.Product)) {
Stop-PSFFunction -EnableException:$EnableException -Message "When piping, please do not use OperatingSystem or Product filters. It's assumed that you are piping the results that you wish to download, so unexpected results may occur."
return
}
Write-PSFMessage -Level Verbose -Message "Source set to $Source"
foreach ($kb in $Pattern) {
if ($Latest) {
$simple = $false
} else {
$simple = $true
}
$params = @{
Pattern = $kb
Architecture = $Architecture
OperatingSystem = $OperatingSystem
Product = $Product
Language = $Language
EnableException = $EnableException
Simple = $Simple
Latest = $Latest
}
if ($PSBoundParameters.Source) {
$params.Source = $Source
}
$inputobjects += Get-KbUpdate @params
}
$inputobjects = $inputobjects | Sort-Object -Unique
foreach ($object in $inputobjects) {
if ($Architecture) {
$templinks = @()
foreach ($arch in $Architecture) {
$templinks += $object.Link | Where-Object { $PSItem -match "$($arch)_" }
if ("x64" -eq $arch) {
$templinks += $object.Link | Where-Object { $PSItem -match "64_" }
$templinks = $templinks | Where-Object { $PSItem -notmatch "-rt-" }
}
if (-not $templinks) {
$templinks += $object | Where-Object Architecture -eq $arch | Select-Object -ExpandProperty Link
}
}
if ($templinks) {
$object.Link = ($templinks | Sort-Object -Unique)
} else {
Stop-PSFFunction -EnableException:$EnableException -Message "Could not find architecture match, downloading all"
}
}
foreach ($dl in $object) {
$title = $dl.Title
$hyperlinklol = $object.Link
if (-not $PSBoundParameters.FilePath) {
$FilePath = Split-Path -Path $hyperlinklol -Leaf
} else {
if (-not $Path) {
$Path = Split-Path -Path $FilePath
}
}
$file = Join-Path -Path $Path -ChildPath $FilePath
if ((Test-Path -Path $file) -and -not $AllowClobber) {
Get-ChildItem -Path $file
continue
}
if ((Get-Command Start-BitsTransfer -ErrorAction Ignore)) {
try {
$filename = Split-Path $hyperlinklol -Leaf
if ((Get-BitsTransfer | Where-Object Description -match kbupdate).FileList.RemoteName -notcontains $hyperlinklol) {
Write-PSFMessage -Level Verbose -Message "Adding $filename to download queue"
$jobs += Start-BitsTransfer -Asynchronous -Source $hyperlinklol -Destination $file -ErrorAction Stop -Description "kbupdate - $title"
}
} catch {
foreach ($hyperlink in $hyperlinklol) {
Write-Progress -Activity "Downloading $FilePath" -Id 1
Write-PSFMessage -Level Verbose -Message "That failed, trying Invoke-WebRequest"
$null = Invoke-TlsWebRequest -OutFile $file -Uri "$hyperlink"
Write-Progress -Activity "Downloading $FilePath" -Id 1 -Completed
}
}
} else {
try {
# IWR is crazy slow for large downloads
Write-Progress -Activity "Downloading $FilePath" -Id 1
$null = Invoke-TlsWebRequest -OutFile $file -Uri "$hyperlinklol"
Write-Progress -Activity "Downloading $FilePath" -Id 1 -Completed
} catch {
Stop-PSFFunction -EnableException:$EnableException -Message "Failure" -ErrorRecord $_ -Continue
}
if ((Test-Path -Path $file)) {
Get-ChildItem -Path $file
}
}
if ((Test-Path -Path $file)) {
Get-ChildItem -Path $file
}
}
}
}
}
if ($jobs) {
Write-PSFMessage -Level Verbose -Message "Starting job process"
$jobs | Start-BitsJobProcess
}
Write-Progress -Activity "Queuing up downloads" -Completed
}
}