-
Notifications
You must be signed in to change notification settings - Fork 18
/
Install-LSUpdate.ps1
202 lines (180 loc) Β· 11.1 KB
/
Install-LSUpdate.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
ο»Ώfunction Install-LSUpdate {
<#
.SYNOPSIS
Installs a Lenovo update package. Downloads it if not previously downloaded.
.DESCRIPTION
Installs a Lenovo update package. Downloads it if not previously downloaded.
.PARAMETER Package
The Lenovo package object to install
.PARAMETER Path
If you previously downloaded the Lenovo package to a custom directory, specify its path here so that the package can be found
.PARAMETER SaveBIOSUpdateInfoToRegistry
If a BIOS update is successfully installed, write information about it to 'HKLM\Software\LSUClient\BIOSUpdate'.
This is useful in automated deployment scenarios, especially the 'ActionNeeded' key which will tell you whether a shutdown or reboot is required to apply the BIOS update.
The created registry values will not be deleted by this module, only overwritten on the next installed BIOS Update.
.PARAMETER Proxy
Specifies the URL of a proxy server to use for the connection to the update repository.
Used if a package still needs to be downloaded before it can be installed.
.PARAMETER ProxyCredential
Specifies a user account that has permission to use the proxy server that is specified by the -Proxy
parameter.
.PARAMETER ProxyUseDefaultCredentials
Indicates that the cmdlet uses the credentials of the current user to access the proxy server that is
specified by the -Proxy parameter.
#>
[CmdletBinding()]
[OutputType('PackageInstallResult')]
Param (
[Parameter( Position = 0, ValueFromPipeline = $true, Mandatory = $true )]
[pscustomobject]$Package,
[ValidateScript({ Test-Path -LiteralPath $_ -PathType Container })]
[System.IO.DirectoryInfo]$Path = "$env:TEMP\LSUPackages",
[switch]$SaveBIOSUpdateInfoToRegistry,
[Uri]$Proxy = $script:LSUClientConfiguration.Proxy,
[pscredential]$ProxyCredential = $script:LSUClientConfiguration.ProxyCredential,
[switch]$ProxyUseDefaultCredentials = $script:LSUClientConfiguration.ProxyUseDefaultCredentials
)
begin {
if ($PSBoundParameters['Debug'] -and $DebugPreference -eq 'Inquire') {
Write-Verbose "Adjusting the DebugPreference to 'Continue'."
$DebugPreference = 'Continue'
}
}
process {
foreach ($PackageToProcess in $Package) {
$Extracter = $PackageToProcess.Files | Where-Object { $_.Kind -eq 'Installer' }
$PackageDirectory = Join-Path -Path $Path -ChildPath $PackageToProcess.ID
if (-not (Test-Path -LiteralPath $PackageDirectory -PathType Container)) {
$null = New-Item -Path $PackageDirectory -Force -ItemType Directory
}
$SpfParams = @{
'SourceFile' = $Extracter
'Directory' = $PackageDirectory
'Proxy' = $Proxy
'ProxyCredential' = $ProxyCredential
'ProxyUseDefaultCredentials' = $ProxyUseDefaultCredentials
}
$FullPath = Save-PackageFile @SpfParams
if (-not $FullPath) {
Write-Error "The installer of package '$($PackageToProcess.ID)' could not be accessed or found and will be skipped"
continue
}
Expand-LSUpdate -Package $PackageToProcess -WorkingDirectory $PackageDirectory
Write-Verbose "Installing package $($PackageToProcess.ID) ..."
# As a precaution, do not apply runtime limits and kill in-progress installers for packages that are likely firmware updaters
if ($script:LSUClientConfiguration.MaxInstallerRuntime -gt [TimeSpan]::Zero -and (
$PackageToProcess.Installer.Command -like '*winuptp.exe*' -or
$PackageToProcess.Installer.Command -like '*Flash.cmd*' -or
$PackageToProcess.Type -in 'BIOS', 'Firmware' -or
$PackageToProcess.Category -like "*BIOS*" -or
$PackageToProcess.Category -like "*UEFI*" -or
$PackageToProcess.Category -like "*Firmware*" -or
$PackageToProcess.Title -like "*BIOS*" -or
$PackageToProcess.Title -like "*UEFI*" -or
$PackageToProcess.Title -like "*Firmware*" -or
$PackageToProcess.RebootType -eq 5)
) {
Write-Verbose "MaxInstallerRuntime of $($script:LSUClientConfiguration.MaxInstallerRuntime) will not be enforced for this package because it appears to be a BIOS or firmware update"
$MaxInstallerRuntime = [TimeSpan]::Zero
} else {
$MaxInstallerRuntime = $script:LSUClientConfiguration.MaxInstallerRuntime
}
switch ($PackageToProcess.Installer.InstallType) {
'CMD' {
# Special-case ThinkPad and ThinkCentre (winuptp.exe and Flash.cmd/wflash2.exe)
# BIOS updates because we can install them silently and unattended with custom arguments
# Other BIOS updates are not classified as unattended and will be treated like any other package.
if ($PackageToProcess.Installer.Command -match 'winuptp\.exe|Flash\.cmd') {
# We are dealing with a known kind of BIOS Update
$installProcess = Install-BiosUpdate -PackageDirectory $PackageDirectory
} else {
# Correct typo from Lenovo ... yes really...
$InstallCMD = $PackageToProcess.Installer.Command -replace '-overwirte', '-overwrite'
$installProcess = Invoke-PackageCommand -Path $PackageDirectory -Command $InstallCMD -RuntimeLimit $MaxInstallerRuntime
}
$Success = $installProcess.Err -eq [ExternalProcessError]::NONE -and $(
if ($installProcess.Info -is [BiosUpdateInfo] -and $null -ne $installProcess.Info.SuccessOverrideValue) {
$installProcess.Info.SuccessOverrideValue
} else {
$installProcess.Info.ExitCode -in $PackageToProcess.Installer.SuccessCodes
}
)
$FailureReason = if ($installProcess.Err) {
"$($installProcess.Err)"
} elseif ($installProcess.Info.ExitCode -in $PackageToProcess.Installer.CancelCodes) {
'CANCELLED_BY_USER'
} elseif (-not $Success) {
'INSTALLER_EXITCODE'
} else {
''
}
$PendingAction = if (-not $Success) {
'NONE'
} elseif ($installProcess.Info -is [BiosUpdateInfo]) {
if ($installProcess.Info.ActionNeeded -eq 'SHUTDOWN') {
'SHUTDOWN'
} elseif ($installProcess.Info.ActionNeeded -eq 'REBOOT') {
'REBOOT_MANDATORY'
}
} elseif ($PackageToProcess.RebootType -eq 0) {
'NONE'
} elseif ($PackageToProcess.RebootType -eq 3) {
'REBOOT_SUGGESTED'
} elseif ($PackageToProcess.RebootType -eq 5) {
'REBOOT_MANDATORY'
}
[PackageInstallResult]@{
ID = $PackageToProcess.ID
Title = $PackageToProcess.Title
Type = $PackageToProcess.Type
Success = $Success
FailureReason = $FailureReason
PendingAction = $PendingAction
ExitCode = $installProcess.Info.ExitCode
StandardOutput = $installProcess.Info.StandardOutput
StandardError = $installProcess.Info.StandardError
LogOutput = if ($installProcess.Info -is [BiosUpdateInfo]) { $installProcess.Info.LogMessage } else { '' }
Runtime = if ($installProcess.Info) { $installProcess.Info.Runtime } else { [TimeSpan]::Zero }
}
# Extra handling for BIOS updates
if ($installProcess.Info -is [BiosUpdateInfo]) {
if ($Success) {
# BIOS Update successful
Write-Information -MessageData "BIOS UPDATE SUCCESS: An immediate full $($installProcess.Info.ActionNeeded) is strongly recommended to allow the BIOS update to complete!" -InformationAction Continue
if ($SaveBIOSUpdateInfoToRegistry) {
Set-BIOSUpdateRegistryFlag -Timestamp $installProcess.Info.Timestamp -ActionNeeded $installProcess.Info.ActionNeeded -PackageHash $Extracter.Checksum
}
}
}
}
'INF' {
$InfSuccessCodes = @(0, 3010) + $PackageToProcess.Installer.SuccessCodes
$InfInstallParams = @{
'Path' = $PackageDirectory
'Executable' = "${env:SystemRoot}\system32\pnputil.exe"
'Arguments' = "/add-driver $($PackageToProcess.Installer.InfFile) /install"
'RuntimeLimit' = $MaxInstallerRuntime
}
$installProcess = Invoke-PackageCommand @InfInstallParams
$Success = $installProcess.Err -eq [ExternalProcessError]::NONE -and $installProcess.Info.ExitCode -in $InfSuccessCodes
[PackageInstallResult]@{
ID = $PackageToProcess.ID
Title = $PackageToProcess.Title
Type = $PackageToProcess.Type
Success = $Success
FailureReason = if ($installProcess.Err) { "$($installProcess.Err)" } elseif (-not $Success) { 'INSTALLER_EXITCODE' } else { '' }
PendingAction = if ($Success -and $installProcess.Info.ExitCode -eq 3010) { 'REBOOT_SUGGESTED' } else { 'NONE' }
ExitCode = $installProcess.Info.ExitCode
StandardOutput = $installProcess.Info.StandardOutput
StandardError = $installProcess.Info.StandardError
LogOutput = ''
Runtime = if ($installProcess.Info) { $installProcess.Info.Runtime } else { [TimeSpan]::Zero }
}
}
default {
Write-Warning "Unsupported package installtype '$_', skipping installation!"
}
}
}
}
}