Advertisement
Madmacs77

Update_Lenovo_BIOS

Aug 3rd, 2018
890
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <#
  2. .SYNOPSIS
  3.     Invoke Lenovo BIOS Update process.
  4. .DESCRIPTION
  5.     This script will invoke the Lenovo BIOS update process for the executable residing in the path specified for the Path parameter.
  6. .PARAMETER Path
  7.     Specify the path containing the WinUPTP or Flash.cmd
  8. .PARAMETER Password
  9.     Specify the BIOS password if necessary.
  10. .PARAMETER LogFileName
  11.     Set the name of the log file produced by the flash utility.
  12. .EXAMPLE
  13.     .\UpdateLenovoBIOSUpdate.ps1 -Path %LenovoBIOSFiles% -LogFileName "LogFileName.log"
  14. .NOTES
  15.     FileName:    UpdateLenovoBIOSUpdate.ps1
  16.     Created:     2018-06-19
  17.     Adapted from Invoke-LenovoBios.ps1 from Maurice Daly
  18.    
  19.     Version history:
  20.     1.0.0 - (2018-06-19) Script created
  21.     1.0.1 - (2018.06.26) Updated testing to try and make sure 32-bit updates didn't happen in WinPE
  22.  
  23. #>
  24. [CmdletBinding(SupportsShouldProcess = $true)]
  25. param (
  26.     [parameter(Mandatory = $true, HelpMessage = "Specify the path containing the Flash64W.exe and BIOS executable.")]
  27.     [ValidateNotNullOrEmpty()]
  28.     [string]$Path,
  29.     [parameter(Mandatory = $false, HelpMessage = "Specify the BIOS password if necessary.")]
  30.     [ValidateNotNullOrEmpty()]
  31.     [string]$Password,
  32.     [parameter(Mandatory = $false, HelpMessage = "Set the name of the log file produced by the flash utility.")]
  33.     [ValidateNotNullOrEmpty()]
  34.     [string]$LogFileName = "LenovoFlashBiosUpdate.log"
  35. )
  36. Begin
  37. {
  38.     # Load Microsoft.SMS.TSEnvironment COM object
  39.     try
  40.     {
  41.         $TSEnvironment = New-Object -ComObject Microsoft.SMS.TSEnvironment -ErrorAction Stop
  42.     }
  43.     catch [System.Exception] {
  44.         Write-Warning -Message "Unable to construct Microsoft.SMS.TSEnvironment object"
  45.     }
  46. }
  47. Process
  48. {
  49.     # Functions
  50.     function Write-CMLogEntry
  51.     {
  52.         param (
  53.             [parameter(Mandatory = $true, HelpMessage = "Value added to the log file.")]
  54.             [ValidateNotNullOrEmpty()]
  55.             [string]$Value,
  56.             [parameter(Mandatory = $true, HelpMessage = "Severity for the log entry. 1 for Informational, 2 for Warning and 3 for Error.")]
  57.             [ValidateNotNullOrEmpty()]
  58.             [ValidateSet("1", "2", "3")]
  59.             [string]$Severity,
  60.             [parameter(Mandatory = $false, HelpMessage = "Name of the log file that the entry will written to.")]
  61.             [ValidateNotNullOrEmpty()]
  62.             [string]$FileName = "Invoke-LenovoBIOSUpdate.log"
  63.         )
  64.         # Determine log file location
  65.         $LogFilePath = Join-Path -Path $Script:TSEnvironment.Value("_SMSTSLogPath") -ChildPath $FileName
  66.        
  67.         # Construct time stamp for log entry
  68.         $Time = -join @((Get-Date -Format "HH:mm:ss.fff"), "+", (Get-WmiObject -Class Win32_TimeZone | Select-Object -ExpandProperty Bias))
  69.        
  70.         # Construct date for log entry
  71.         $Date = (Get-Date -Format "MM-dd-yyyy")
  72.        
  73.         # Construct context for log entry
  74.         $Context = $([System.Security.Principal.WindowsIdentity]::GetCurrent().Name)
  75.        
  76.         # Construct final log entry
  77.         $LogText = "<![LOG[$($Value)]LOG]!><time=""$($Time)"" date=""$($Date)"" component=""LenovoBIOSUpdate.log"" context=""$($Context)"" type=""$($Severity)"" thread=""$($PID)"" file="""">"
  78.        
  79.         # Add value to log file
  80.         try
  81.         {
  82.             Out-File -InputObject $LogText -Append -NoClobber -Encoding Default -FilePath $LogFilePath -ErrorAction Stop
  83.         }
  84.         catch [System.Exception] {
  85.             Write-Warning -Message "Unable to append log entry to Invoke-LenovoBIOSUpdate.log file. Error message: $($_.Exception.Message)"
  86.         }
  87.     }
  88.    
  89.     cd $Path
  90.     # Write log file for script execution
  91.     Write-CMLogEntry -Value "Initiating script to determine flashing capabilities for Lenovo BIOS updates" -Severity 1
  92.    
  93.     ## DETERMINE BIOS UPDATE METHOD
  94.     if (Test-Path "$Path\WinUPTP*.exe")
  95.     {
  96.     Write-CMLogEntry -Value "Using WinUTPT BIOS update method" -Severity 1
  97.         if (Test-Path "$Path\WinUPTP64.exe")
  98.         {
  99.             Write-CMLogEntry -Value "Using WinUTPT 64-bit" -Severity 1
  100.             $FlashSwitches = " /S"
  101.             $FlashUtility = "$Path\\WinUPTP64.exe"
  102.         }
  103.         else
  104.         {
  105.             Write-CMLogEntry -Value "Using WinUTPT 32-bit. Setting TS Variable for full-OS update of BIOS" -Severity 1
  106.             $FlashSwitches = " /S"
  107.             $FlashUtility = "$Path\\WinUPTP.exe"
  108.             $TSEnvironment.Value("BIOSFULLOS") = $true
  109.         }
  110.     }
  111.  
  112.     if (Test-Path "$Path\Flash*.cmd")
  113.     {
  114.     Write-CMLogEntry -Value "Using Flash BIOS update method" -Severity 1
  115.         if (Test-Path "$Path\Flash64.cmd")
  116.         {
  117.             Write-CMLogEntry -Value "Using Flash 64-bit CMD file" -Severity 1
  118.             $FlashSwitches = " /quiet /sccm /ign"
  119.             $FlashUtility = "$Path\\Flash64.cmd"
  120.             $TSEnvironment.Value("BIOSSHUTDOWN") = $true
  121.         }
  122.         else
  123.         {
  124.             Write-CMLogEntry -Value "Using Flash 32-bit CMD file. Setting TS Variable for full-OS update of BIOS" -Severity 1
  125.             $FlashSwitches = " /quiet /sccm /ign"
  126.             $FlashUtility = "$Path\\Flash.cmd"
  127.             $TSEnvironment.Value("BIOSFULLOS") = $true
  128.             $TSEnvironment.Value("BIOSSHUTDOWN") = $true
  129.         }
  130.     }
  131.    
  132.     # Set log file location
  133.     $LogFilePath = Join-Path -Path $TSEnvironment.Value("_SMSTSLogPath") -ChildPath $LogFileName
  134.    
  135.  
  136.     ## BIOS UPDATE PROCESS    
  137.     ## Apply 64-bit updates in WinPE
  138.     if (($TSEnvironment -ne $null) -and ($TSEnvironment.Value("_SMSTSinWinPE") -eq $true) -and ($TSEnvironment.Value("BIOSFULLOS") -ne $true))
  139.     {      
  140.         try
  141.         {
  142.             # Start flash update process
  143.             $FlashProcess = Start-Process -FilePath $FlashUtility -ArgumentList "$FlashSwitches" -Passthru -Wait
  144.            
  145.             #Output Exit Code for testing purposes
  146.             $FlashProcess.ExitCode | Out-File -FilePath $LogFilePath
  147.  
  148.             #Get winuptp.log file
  149.             if (Test-Path "$Path\WinUPTP64.exe")
  150.             {
  151.                 $winuptplog = Get-ChildItem -Filter "*.log" -Recurse | Where-Object { $_.Name -like "winuptp.log" } -ErrorAction SilentlyContinue| Select-Object -ExpandProperty FullName
  152.                 Write-CMLogEntry -Value "winuptp.log file path is $($winuptplog)" -Severity 1
  153.                 $smstslogpath = Join-Path -Path $TSEnvironment.Value("_SMSTSLogPath") -ChildPath "winuptp.log"         
  154.                 Copy-Item -Path $winuptplog -Destination $smstslogpath -Force -ErrorAction SilentlyContinue
  155.             }
  156.         }
  157.         catch [System.Exception] {
  158.             Write-CMLogEntry -Value "An error occured while updating the system BIOS in OS online phase. Error message: $($_.Exception.Message)" -Severity 3; exit 1
  159.         }
  160.     }
  161.     ##Apply 32-bit Updates in Windows
  162.     if (($TSEnvironment -ne $null) -and ($TSEnvironment.Value("_SMSTSinWinPE") -ne $true) -and ($TSEnvironment.Value("BIOSFULLOS") -eq $true))
  163.     {
  164.         # Used in a later section of the task sequence
  165.        
  166.         # Detect Bitlocker Status
  167.         $OSVolumeEncypted = if ((Manage-Bde -Status C:) -match "Protection On") { Write-Output $True }
  168.         else { Write-Output $False }
  169.        
  170.         # Supend Bitlocker if $OSVolumeEncypted is $true
  171.         if ($OSVolumeEncypted -eq $true)
  172.         {
  173.             Write-CMLogEntry -Value "Suspending BitLocker protected volume: C:" -Severity 1
  174.             Manage-Bde -Protectors -Disable C:
  175.         }
  176.        
  177.         # Start Bios update process
  178.         try
  179.         {
  180.             Write-CMLogEntry -Value "Running Flash Update - $FlashUtility" -Severity 1
  181.             $FlashProcess = Start-Process -FilePath $FlashUtility -ArgumentList "$FlashSwitches" -Passthru -Wait
  182.            
  183.             #Output Exit Code for testing purposes
  184.             $FlashProcess.ExitCode | Out-File -FilePath $LogFilePath
  185.         }
  186.         catch [System.Exception]
  187.         {
  188.             Write-Warning -Message "An error occured while updating the system bios. Error message: $($_.Exception.Message)"; exit 1
  189.         }
  190.     }
  191. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement