-
Notifications
You must be signed in to change notification settings - Fork 1
/
Invoke-TBFirmware.ps1
217 lines (178 loc) · 8.96 KB
/
Invoke-TBFirmware.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
<#
.SYNOPSIS
Invoke BIOS Update process.
.DESCRIPTION
This script will invoke a Thunderbolt Firmware update process for a varity of manufactures. This process should be ran in WINPE.
.PARAMETER LogFileName
Set the name of the log file produced by the flash utility.
.EXAMPLE
.NOTES
FileName: Update-TBFirmware.ps1
Author: Richard tracy
Contact: [email protected]
Created: 2018-08-24
Inspired: Anton Romanyuk,Nickolaj Andersen
Version history:
1.0.0 - (2018-08-24) Script created
#>
##*===========================================================================
##* FUNCTIONS
##*===========================================================================
function Write-LogEntry {
param(
[parameter(Mandatory=$true, HelpMessage="Value added to the log file.")]
[ValidateNotNullOrEmpty()]
[string]$Value,
[parameter(Mandatory=$false)]
[ValidateSet(0,1,2,3)]
[int16]$Severity,
[parameter(Mandatory=$false, HelpMessage="Name of the log file that the entry will written to.")]
[ValidateNotNullOrEmpty()]
[string]$fileArgName = $LogFilePath,
[parameter(Mandatory=$false)]
[switch]$Outhost
)
[string]$LogTime = (Get-Date -Format 'HH:mm:ss.fff').ToString()
[string]$LogDate = (Get-Date -Format 'MM-dd-yyyy').ToString()
[int32]$script:LogTimeZoneBias = [timezone]::CurrentTimeZone.GetUtcOffset([datetime]::Now).TotalMinutes
[string]$LogTimePlusBias = $LogTime + $script:LogTimeZoneBias
# Get the file name of the source script
Try {
If ($script:MyInvocation.Value.ScriptName) {
[string]$ScriptSource = Split-Path -Path $script:MyInvocation.Value.ScriptName -Leaf -ErrorAction 'Stop'
}
Else {
[string]$ScriptSource = Split-Path -Path $script:MyInvocation.MyCommand.Definition -Leaf -ErrorAction 'Stop'
}
}
Catch {
$ScriptSource = ''
}
If(!$Severity){$Severity = 1}
$LogFormat = "<![LOG[$Value]LOG]!>" + "<time=`"$LogTimePlusBias`" " + "date=`"$LogDate`" " + "component=`"$ScriptSource`" " + "context=`"$([Security.Principal.WindowsIdentity]::GetCurrent().Name)`" " + "type=`"$Severity`" " + "thread=`"$PID`" " + "file=`"$ScriptSource`">"
# Add value to log file
try {
Out-File -InputObject $LogFormat -Append -NoClobber -Encoding Default -FilePath $LogFilePath -ErrorAction Stop
}
catch [System.Exception] {
Write-LogEntry -Message "Unable to append log entry to $LogFilePath file"
}
If($Outhost){
Switch($Severity){
0 {Write-Host $Value -ForegroundColor Gray}
1 {Write-Host $Value}
2 {Write-Warning $Value}
3 {Write-Host $Value -ForegroundColor Red}
default {Write-Host $Value}
}
}
}
##*===========================================================================
##* VARIABLES
##*===========================================================================
## Instead fo using $PSScriptRoot variable, use the custom InvocationInfo for ISE runs
If (Test-Path -LiteralPath 'variable:HostInvocation') { $InvocationInfo = $HostInvocation } Else { $InvocationInfo = $MyInvocation }
[string]$scriptDirectory = Split-Path -Path $InvocationInfo.MyCommand.Definition -Parent
[string]$scriptPath = $InvocationInfo.MyCommand.Definition
[string]$scriptName = [IO.Path]::GetFileNameWithoutExtension($scriptPath)
#Create Paths
$TBPath = Join-Path $scriptDirectory -ChildPath TB16
$TempPath = Join-Path $scriptDirectory -ChildPath Temp
$ToolsPath = Join-Path $scriptDirectory -ChildPath Tools
Try
{
$tsenv = New-Object -COMObject Microsoft.SMS.TSEnvironment
#$logPath = $tsenv.Value("LogPath")
$LogPath = $tsenv.Value("_SMSTSLogPath")
$tsenv.Value("SMSTS_TBUpdate") = "True"
$inPE = $tsenv.Value("_SMSTSInWinPE")
}
Catch
{
Write-Warning "TS environment not detected. Assuming stand-alone mode."
$LogPath = $env:TEMP
}
[string]$fileArgName = $scriptName +'.log'
$LogFilePath = Join-Path -Path $LogPath -ChildPath $fileArgName
##*===========================================================================
##* MAIN
##*===========================================================================
#Get Supported Models from File
#https://www.dell.com/en-us/work/shop/dell-business-thunderbolt-dock-tb16-with-240w-adapter/apd/452-bcnu/pc-accessories
[array]$SupportedModels = Get-Content ModelsSupported.txt -ErrorAction SilentlyContinue
#Create Model Variable
$ComputerModel = Get-WmiObject -Class Win32_computersystem | Select-Object -ExpandProperty Model
#determine if model support thunderbolt
Write-LogEntry ("Comparing this model [{0}] with supported model list [{1}]" -f $ComputerModel,"ModelsSupported.txt") -Outhost
Foreach ($SupportedModel in $SupportedModels){
If($ComputerModel -eq $SupportedModel){
#Get Thunderbolt Firmware File Name
$TBFirmwareFileName = Get-ChildItem $TBPath -Recurse -Filter *.exe | Where-Object {$_.Name -match 'Fw' -or $_.Name -match 'Firmware'} | Select -First 1
If($TBFirmwareFileName){
#Copy TB Installer to the root of the package - the Flash64W didn't like when I left it in the Computer Model folder, because it has spaces. (Yes, I tried qoutes and stuff)
Copy-Item $TBFirmwareFileName.FullName -Destination $TempPath -ErrorAction SilentlyContinue
#build temp path
$FMWFilePath = Join-Path -Path $TempPath -ChildPath $TBFirmwareFileName.Name
#Get TB File Name (No Extension, used to create Log File)
$TBLogFileName = Get-ChildItem $FMWFilePath -Verbose | Select -ExpandProperty BaseName
$TBLogFileName = $TBLogFileName + ".log"
#Get TB Password from File
$BiosPassword = Get-Content .\BIOSPassword.txt -ErrorAction SilentlyContinue
#Set Command Arguments for TB Update
If($BiosPassword){
$AddArgs = "/s /p=$TBPassword /l=$LogPath\$TBLogFileName"
}
else {
$AddArgs = "/s /l=$LogPath\$TBLogFileName"
}
if ($tsenv -and $inPE) {
Write-LogEntry "TaskSequence is running in Windows Preinstallation Environment (PE)" -Outhost
}
Else{
Write-LogEntry "TaskSequence is running in Windows Environment" -Outhost
# Detect Bitlocker Status
$OSVolumeEncypted = if ((Manage-Bde -Status C:) -match "Protection On") { Write-Output $true } else { Write-Output $false }
# Supend Bitlocker if $OSVolumeEncypted is $true
if ($OSVolumeEncypted -eq $true) {
Write-LogEntry "Suspending BitLocker protected volume: C:" -Outhost
Manage-Bde -Protectors -Disable C:
}
}
#execute flashing
If($BiosPassword){$protectedArgs = $($AddArgs -replace $BiosPassword, "<Password Removed>")}Else{$protectedArgs = $AddArgs}
Write-LogEntry "RUNNING COMMAND : $FMWFilePath $fileArg $AddArgs" -Outhost
$Process = Start-Process $FMWFilePath -ArgumentList $AddArgs -PassThru -wait
#Creates and Set TS Variable to be used to run additional steps if reboot required.
switch($process.ExitCode){
0 {
Write-LogEntry ("Thunderbolt Firmware updated succesfully") -Outhost
If($tsenv){$tsenv.Value("SMSTS_TBRebootRequired") = "False"}
If($tsenv){$tsenv.Value("SMSTS_TBBatteryCharge") = "False"}
}
2 {
Write-LogEntry ("Thunderbolt 16 updated succesfully. A reboot is required") -Outhost
If($tsenv){$tsenv.Value("SMSTS_TBRebootRequired") = "True"}
}
10 {
Write-LogEntry ("Thunderbolt 16 cannot update because it requires an earlier release first") -Outhost
Write-LogEntry ("OR Thunderbolt 16 cannot update because the battery is missing or not charged") -Outhost
Write-LogEntry ("OR Thunderbolt 16 update was cancelled") -Outhost
If($tsenv){$tsenv.Value("SMSTS_TBBatteryCharge") = "True"}
}
}
Start-Sleep 10
#remove exe after completed
Remove-Item $TBFilePath -Force -ErrorAction SilentlyContinue
If($process.ExitCode -eq 2){
Write-LogEntry ("Since Thunderbolt 16 firmware updated correctly and requires a reboot, stopping loop process to install additional Thunderbolt software until later") -Outhost
Exit
}
}
Else{
Write-LogEntry ("No Firmware Found in folder: {0}, skipping..." -f $TBPath) -Outhost
}
}
Else{
Write-LogEntry ("Model is not a [{0}]. Thunderbolt Docking Station is not supported, skipping installation" -f $SupportedModel) -Outhost
}
}