This repository has been archived by the owner on Mar 4, 2022. It is now read-only.
forked from julrich-usgs/nofips-dell-drivers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DellUpdateFunctions.ps1
125 lines (108 loc) · 4.2 KB
/
DellUpdateFunctions.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
# DellUpdateFunctions.ps1
#
# Required module for nofips-dell-drivers. Does nothing by itself.
#
# Shared Variables:
$downloadLocation = $PSScriptRoot + "\DellDrivers\"
$processlog = $downloadLocation + "process.log"
$installScript = $downloadLocation + "install.ps1"
$xmlcatalog = $downloadLocation + "driverUpdates.xml"
$dcucli = $PSScriptRoot + "\dcu-cli.exe"
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
# Writes a message to the console, a file, and the GUI!
Function LogNote([string] $message){
Write-Host $message
Add-Content $processlog $message
if ($global:gui){
$Note.Text = $message
}
}
# Runs Dell Command Update and makes XML report of available updates
Function CheckForUpdates() {
# Initialize the update location
mkdir -Path $downloadLocation -Force
New-Item -ItemType "file" -Path $processlog -Force
New-Item -ItemType "file" -Path $xmlcatalog -Force
# Required to pass local variables to background job
$scriptBlok = {
param ($dcucli, $xmlcatalog)
Start-Process -Wait -FilePath $dcucli -ArgumentList "/report $xmlcatalog" -WindowStyle hidden
}
# Check for update as a background job
Start-Job -ScriptBlock $scriptBlok -ArgumentList $dcucli, $xmlcatalog
LogNote "Checking System for Updates. Please wait... "
SpinWait
LogNote "Dell Command Update Client Finished"
}
# Downloads all the update files and makes the Installation Script, takes an arraylist of String
Function DownloadUpdates($urlist){
New-Item -ItemType "file" -Path $installScript -Force
ForEach($url in $urlist) {
$downloadFile = $url -Split "/"
$downloadFile = $downloadFile[$downloadFile.length - 1]
$filePath = $downloadLocation + $downloadFile
LogNote "Attempting download $url"
# Download as a background job
Start-Job -ScriptBlock ${function:DownloadWithRetry} -ArgumentList $url, $filePath, 5
# Add command to install script
Add-Content $installScript "Start-Process -Wait -FilePath `"$filePath`" -ArgumentList `"/s`", `"/l=```"$filePath.log```"`""
}
SpinWait
}
# Download the file specified by url to the out file location, can retry
function DownloadWithRetry([string] $url, [string] $outFile, [int] $retries)
{
while($true)
{
try
{
Invoke-WebRequest $url -OutFile $outFile -UseBasicParsing
Write-Verbose "Download succeeded"
break
}
catch
{
$exceptionMessage = $_.Exception.Message
Write-Verbose "Failed to download '$url': $exceptionMessage"
if ($retries -gt 0) {
$retries--
Write-Verbose "Waiting 10 seconds before retrying. Retries left: $retries"
Start-Sleep -Seconds 10
}
else
{
Write-Verbose "Max retries reached download failed $url"
throw $_.Exception
}
}
}
}
# Runs the install script as background job
Function InstallUpdates(){
Start-Job -FilePath $installScript
Write-Host | Get-Content $installScript
LogNote "Installing updates... "
SpinWait
LogNote "Installation Finished! Reboot may be required."
}
# SpinWait prevents GUI lockup due to running Powershell commands on a WinForms thread
function SpinWait() {
[System.Windows.Forms.Application]::UseWaitCursor=$true
if ($global:gui){$oldNote = $Note.Text}
$spinner = ".oO@+-\|/-"
While (@(Get-Job | Where-Object { $_.State -eq "Running" }).Count -ne 0)
{
Start-Sleep -Milliseconds 222
if ($global:gui){
$Note.Text = $oldNote + $spinner.Substring($spinStep, 1)
$spinStep++
if ($spinStep -gt $spinner.Length - 1) {$spinStep = 0}
}
# TODO: Replace DoEvents with an async await Receive-Job (I couldn't find a way to do this)
[System.Windows.Forms.Application]::DoEvents()
}
# Clean up jobs
Remove-Job -State Completed
Remove-Job -State Failed
[System.Windows.Forms.Application]::UseWaitCursor=$false
}