-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathStartSoftPaqDownloads.ps1
353 lines (275 loc) · 10.4 KB
/
StartSoftPaqDownloads.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
<#
Start SoftPaq Downloads
Copyright © 2015-2018 Michael 'Tex' Hex
Licensed under the Apache License, Version 2.0.
https://github.com/texhex/BiosSledgehammer
#>
#Script version
$scriptversion = "1.1.4"
#This script requires PowerShell 4.0 or higher
#requires -version 4.0
#Require full level Administrator
#requires -runasadministrator
#Guard against common code errors
Set-StrictMode -version 2.0
#Terminate script on errors
$ErrorActionPreference = 'Stop'
#Import Module with some helper functions
Import-Module $PSScriptRoot\MPSXM.psm1 -Force
function Get-UserConfirm()
{
param(
[Parameter(Mandatory = $True, ValueFromPipeline = $True)]
[ValidateNotNullOrEmpty()]
[string]$BaseFolder
)
#from https://social.technet.microsoft.com/Forums/scriptcenter/en-US/3d8f242b-199b-4d4c-b973-0246ce1c065c/windows-powershell-tip-of-the-week-is-there-an-easy-way-to-display-and-process-confirmation?forum=ITCG
#by Shay Levi (https://social.technet.microsoft.com/profile/shay%20levi)
$caption = "BIOS Sledgehammer: Start SoftPaq Downloads v$scriptversion"
$message = "This script will download firmware update files from HP.com,`nbased on the SPDownload.txt files found in [$BaseFolder].`nStart downloads?"
$yes = new-Object System.Management.Automation.Host.ChoiceDescription "&Yes", "Start downloads"
$no = new-Object System.Management.Automation.Host.ChoiceDescription "&No", "Do not start, stop script"
$choices = [System.Management.Automation.Host.ChoiceDescription[]]($yes, $no)
$answer = $host.ui.PromptForChoice($caption, $message, $choices, 1)
switch ($answer)
{
0
{
return $true
break
}
1
{
return $false
break
}
}
}
function Test-FolderStructure()
{
param(
[Parameter(Mandatory = $True, ValueFromPipeline = $True)]
[ValidateNotNullOrEmpty()]
[string]$SearchPath
)
if ( -not (Test-DirectoryExists "$SearchPath\Models") )
{
throw New-Exception -DirectoryNotFound "Path [$SearchPath\Models] does not exist"
}
if ( -not (Test-DirectoryExists "$SearchPath\Shared") )
{
throw New-Exception -DirectoryNotFound "Path [$SearchPath\Shared] does not exist"
}
if ( -not (Test-DirectoryExists "$SearchPath\PwdFiles") )
{
throw New-Exception -DirectoryNotFound "Path [$SearchPath\PwdFiles] does not exist"
}
if ( -not (Test-FileExists "$SearchPath\BiosSledgehammer.ps1") )
{
throw New-Exception -FileNotFound "File [$SearchPath\BiosSledgehammer.ps1] does not exist"
}
}
function Remove-FileIfExists()
{
param(
[Parameter(Mandatory = $True, ValueFromPipeline = $True)]
[ValidateNotNullOrEmpty()]
[string]$Path
)
if ( Test-FileExists -Path $Path )
{
try
{
#we allow the system half a second...
Start-Sleep -Milliseconds 500
Remove-Item -Path "$Path" -Force
}
catch
{
#Sometimes this does not work because AV or backup software is to eager to scan the file
write-warning "Unable to delete file [$Path] - $($Error[0])"
}
}
}
function Start-DownloadFile()
{
param(
[Parameter(Mandatory = $True, ValueFromPipeline = $True)]
[ValidateNotNullOrEmpty()]
[string]$URL,
[Parameter(Mandatory = $True, ValueFromPipeline = $True)]
[ValidateNotNullOrEmpty()]
[string]$DownloadPath
)
$file = Get-FileName($URL)
#Ensure the files does not exist
$tempFile = "$DownloadPath\$file"
Remove-FileIfExists $tempFile
$webClient = New-Object "Net.WebClient"
write-host " Downloading from [$URL]"
write-host " to [$tempFile]... " -NoNewline
$webClient.DownloadFile($URL, $tempFile)
$webClient = $null
write-host "Done"
return $tempFile
}
function Invoke-AcquireHPFile()
{
param(
[Parameter (Mandatory = $true)]
[ValidateSet("SoftPaq", "ReleaseNotes")]
[string]$Type,
[Parameter(Mandatory = $True)]
[ValidateNotNullOrEmpty()]
[string]$URL,
[Parameter(Mandatory = $True)]
[ValidateNotNullOrEmpty()]
[string]$DestinationPath,
[Parameter(Mandatory = $True)]
[ValidateNotNullOrEmpty()]
[string]$DownloadPath
)
if ( $Type -eq "SoftPaq" )
{
write-host " SoftPaq URL: $URL"
}
else
{
write-host " Release Notes URL: $URL"
}
$destFilename = "$DestinationPath\$(Get-FileName($URL))"
if ( Test-FileExists -Path $destFilename )
{
write-host " File already exists"
}
else
{
$tempFile = Start-DownloadFile -URL $URL -DownloadPath $DownloadPath
if ( $Type -eq "SoftPaq" )
{
$SPName = Get-FileName -Path $tempFile -WithoutExtension
$SPName = $SPName.ToUpper()
write-host " Extracting SoftPaq... " -NoNewline
&$tempFile -e -s
Start-Sleep -Seconds 2 #just to be sure, sometimes it requires some extra time
write-host "Done"
#We are expecting the file to be present in C:\SWSetup\SPxxxx
$SPExtractedPath = "$UNPACK_FOLDER\$SPName"
#SP88497 is extracted to [SP88497, so we better make sure to check that folder as well
$SPExtractedPath2 = "$UNPACK_FOLDER\[$($SPName)"
if ( Test-Path -LiteralPath $SPExtractedPath2 -PathType Container)
{
write-warning "Extracted files folder is $SPExtractedPath2, renaming folder"
Rename-Item -LiteralPath $SPExtractedPath2 -NewName $SPName
}
if ( -not (Test-DirectoryExists -Path $SPExtractedPath) )
{
throw New-Exception -FileNotFound "Unable to locate unpack folder [$SPExtractedPath]"
}
else
{
#now we need to copy the unpacked files
write-host " Copy from [$SPExtractedPath] to [$destFolder]... " -NoNewline
$ignored = Get-ChildItem -Path $SPExtractedPath | Copy-Item -Destination $destFolder -Recurse -Container -Force
write-host "Done"
#Remove extract folder - this will sometimes fail because of backup or AV tools
try
{
$ignored = Remove-Item -LiteralPath $SPExtractedPath -Recurse -Force
}
catch
{
write-warning "Unable to remove temp extraction folder [$SPExtractedPath] - $($Error[0])"
}
}
}
#copy SPXXXX to destination
Copy-FileToDirectory -Filename $tempFile -Directory $DestinationPath
#This would be a good idea in case the SPxxx would not be running sometimes in the background
#and make this call fail. Hence: Skip it.
#Remove-FileIfExists -Path $tempFile
write-host " File processed successfully"
}
}
function Invoke-DownloadProcess()
{
param(
[Parameter(Mandatory = $True, ValueFromPipeline = $True)]
[ValidateNotNullOrEmpty()]
[string]$SettingsFile,
[Parameter(Mandatory = $True, ValueFromPipeline = $True)]
[ValidateNotNullOrEmpty()]
[string]$DownloadPath
)
$destFolder = Get-ContainingDirectory($SettingsFile)
$settings = Read-StringHashtable $SettingsFile
if ($settings.ContainsKey("NoteURL"))
{
$URL = $settings["NoteURL"]
$type = "ReleaseNotes"
Invoke-AcquireHPFile -Type $type -URL $URL -DestinationPath $destFolder -DownloadPath $DownloadPath
}
if ($settings.ContainsKey("SPaqURL"))
{
$URL = $settings["SPaqURL"]
$type = "SoftPaq"
Invoke-AcquireHPFile -Type $type -URL $URL -DestinationPath $destFolder -DownloadPath $DownloadPath
}
}
#Issue #41 - https://github.com/texhex/BiosSledgehammer/issues/41
#
#TLS 1.0 and SSL3 are no longer supported by ftp.hp.com (see http://ssl-checker.online-domain-tools.com/
#and enter ftp.hp.com)
#
#The default enabled security protocols for .NET 4.0/4.5 (and hence PowerShell) are SecurityProtocolType.Tls|SecurityProtocolType.Ssl3.
#
#Therefore we need to turn on TLS 1.1 and TLS 1.2. As TLS 1.3 is also on the horizon, and will properly
#be a default in PowerShell, we only turn on TLS 1.1 and TLS 1.2 without touching the default protocols.
#
#Full details on this StackOverflow answer by Luke Hutton: https://stackoverflow.com/a/28333370
#
Set-HTTPSecurityProtocolSecureDefault
######################################################
## Main ##############################################
######################################################
Set-Variable CHECK_FILENAME "$PSScriptRoot\BiosSledgehammer.ps1" –option ReadOnly -Force
Set-Variable TEMP_DOWNLOAD_FOLDER "$(Get-TempFolder)\TempDownload" –option ReadOnly -Force
Set-Variable UNPACK_FOLDER "C:\SWSetup" –option ReadOnly -Force
if ( Get-UserConfirm -BaseFolder $PSScriptRoot)
{
$ignored = Test-FolderStructure -SearchPath $PSScriptRoot
#ensure the temp downloads folder exists
$ignored = New-Item -Path $TEMP_DOWNLOAD_FOLDER -ItemType Directory -Force
#scan for SPDownload.txt files
$Files = Get-ChildItem -Path $PSScriptRoot -Filter "SPDownload.txt" -Recurse
foreach ($file in $Files)
{
$curFile = $file.Fullname
write-host "File [$curFile]"
Invoke-DownloadProcess -SettingsFile $curFile -DownloadPath $TEMP_DOWNLOAD_FOLDER
}
write-host "All done, waiting 20 seconds before starting clean up..."
Start-Sleep -Seconds 20
write-host "Cleaning up $UNPACK_FOLDER..."
#check if the UNPACK_FOLDER exists and if it's empty. If so, delete it
if ( Test-DirectoryExists $UNPACK_FOLDER )
{
$count = (Get-ChildItem -Path $UNPACK_FOLDER -Directory | Measure-Object).Count
if ( $count -eq 0 )
{
#Folder is empty
$ignored = Remove-Item -Path $UNPACK_FOLDER -Force
}
}
write-host "Cleaning up $TEMP_DOWNLOAD_FOLDER..."
#try to delete the temp folder
try
{
$ignored = Remove-Item -LiteralPath $TEMP_DOWNLOAD_FOLDER -Force -Recurse -Confirm:$false
}
catch
{
Write-Warning "Unable to delete folder [$TEMP_DOWNLOAD_FOLDER] - $($Error[0])"
}
}
write-host "Script finished!"