Advertisement
Guest User

Untitled

a guest
Oct 21st, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Function Get-ScriptDirectory {
  2.     If ($PSISE) {
  3.         Split-Path $PSISE.CurrentFile.FullPath
  4.     }
  5.     Else {
  6.         $Global:PSScriptRoot
  7.     }
  8. }
  9. ##################################
  10.  
  11. #Log file info
  12. $Today = Get-Date -Format yyyy-MM-dd
  13. $Time = Get-Date -Format HH:mm:ss
  14. $LogDir = "$ENV:SystemDrive\Windows\Temp"
  15. $LogFileName = "LOG_HPBiosUpdate.log"
  16. $LogFile = Join-Path $LogDir $LogFileName
  17.  
  18. #Start logging
  19. Out-File $LogFile -Append -InputObject ("")
  20. Out-File $LogFile -Append -InputObject ("")
  21. Out-File $LogFile -Append -InputObject ("$Today - $Time")
  22. Out-File $LogFile -Append -InputObject ("")
  23.  
  24. #Define locations
  25. $ScriptDir = Get-ScriptDirectory
  26. $SaveToRoot = "$ENV:SystemDrive\Windows\Temp"
  27. $SaveToDirectory = "HP_BIOS"
  28. $SaveToLocation = "$SaveToRoot\$SaveToDirectory"
  29. $DownloadDir = "$($SaveToLocation)\HP_BIOS_Downloads"
  30. $ExtractDir = "$($SaveToLocation)\HP_BIOS_Extract"
  31.  
  32. If (!(Test-Path $SaveToLocation)) {
  33.     $null = New-Item -Path $SaveToRoot\ -Name $SaveToDirectory -ItemType "Directory" -Force
  34. }
  35.  
  36. If (!(Test-Path $DownloadDir)) {
  37.     $null = New-Item -Path $SaveToLocation\ -Name "HP_BIOS_Downloads" -ItemType "Directory" -Force
  38. }
  39.  
  40. If (!(Test-Path $ExtractDir)) {
  41.     $null = New-Item -Path $SaveToLocation\ -Name "HP_BIOS_Extract" -ItemType "Directory" -Force
  42. }
  43.  
  44. #Define variables
  45. $OS = "Win10"
  46. $Category = "BIOS"
  47. $ProductCode = (Get-WmiObject -Class Win32_BaseBoard).Product
  48. $Model = (Get-WmiObject -Class Win32_ComputerSystem).Model
  49. $HP_CMSL_URL = "https://ftp.hp.com/pub/caps-softpaq/cmit/release/cmsl/hp-cmsl-latest.exe"
  50.  
  51. #Detect HP BIOS PS module
  52. Try {
  53.     Get-HPBiosVersion | Out-Null
  54.     Write-Host "HP Client Management Script Library already installed"
  55.     Out-File $LogFile -Append -InputObject ("HP Client Management Script Library already installed")
  56. }
  57. Catch {
  58.     Write-Host "HP Client Management Script Library not loaded"
  59.     Write-Host "Downloading from $HP_CMSL_URL"
  60.     Out-File $LogFile -Append -InputObject ("HP Client Management Script Library not loaded")
  61.     Out-File $LogFile -Append -InputObject ("Downloading from $HP_CMSL_URL")
  62.     Invoke-WebRequest -Uri $HP_CMSL_URL -OutFile "$($DownloadDir)\HP_CMSL.exe"
  63.     Write-Host "Installing HP Client Management Script Library from $($DownloadDir)\HP_CMSL.exe"
  64.     Out-File $LogFile -Append -InputObject ("Installing HP Client Management Script Library from $($DownloadDir)\HP_CMSL.exe")
  65.     Start-Process -FilePath "$($DownloadDir)\HP_CMSL.exe" -ArgumentList "/Silent /NoCancel /NoRestart /CloseApplications /Log" -Wait
  66.     Write-Host "Finished downloading and installing HP Client Management Script Library"
  67.     Out-File $LogFile -Append -InputObject ("Finished downloading and installing HP Client Management Script Library")
  68. }
  69.  
  70. #Get BIOS info
  71. $CurrentBIOS = Get-HPBiosVersion
  72. Write-Host "Currently installed BIOS: version $($CurrentBIOS)"
  73. Write-Host "Checking product code $($ProductCode) for BIOS updates"
  74. Out-File $LogFile -Append -InputObject ("Currently installed BIOS: version $($CurrentBIOS)")
  75. Out-File $LogFile -Append -InputObject ("Checking product code $($ProductCode) for BIOS updates")
  76. $BIOS = Get-SoftpaqList -Platform $ProductCode -Os $OS -Category $Category
  77. $MostRecent = ($BIOS | Measure-Object -Property "ReleaseDate" -Maximum).Maximum
  78. $BIOS = $BIOS | Where "ReleaseDate" -eq "$MostRecent"
  79.  
  80. If ([VERSION]$CurrentBIOS -ge [VERSION]$BIOS.Version) {
  81.     Write-Host "BIOS already current"
  82.     Out-File $LogFile -Append -InputObject ("BIOS already current")
  83. }
  84. Else {
  85.     Write-Host "Updated BIOS available: version $([VERSION]$BIOS.Version)"
  86.     Out-File $LogFile -Append -InputObject ("Updated BIOS available: version $([VERSION]$BIOS.Version)")
  87.  
  88.     #Create folders
  89.     $DownloadPath = "$($DownloadDir)\$($Model)\$($BIOS.Version)"
  90.     $ExtractPath = "$($ExtractDir)\$($Model)\$($BIOS.Version)"
  91.  
  92.     If (!(Test-Path $DownloadPath)) {
  93.         $null = New-Item $DownloadPath -ItemType Directory -Force
  94.     }
  95.  
  96.     If (!(Test-Path $ExtractPath)) {
  97.         $null = New-Item $ExtractPath -ItemType Directory -Force
  98.     }
  99.  
  100.     #Download BIOS files
  101.     $BIOS_CVA = "$($DownloadPath)\$($BIOS.Id).cva"
  102.     $BIOS_HTML = "$($DownloadPath)\$($BIOS.Id).html"
  103.     $BIOS_EXE = "$($DownloadPath)\$($BIOS.Id).exe"
  104.     $BIOS_TXT = "$($DownloadPath)\$($BIOS.ReleaseDate).txt"
  105.     Write-Host "Downloading BIOS update for model $($Model) / product $($ProductCode) to $($DownloadPath)"
  106.     Out-File $LogFile -Append -InputObject ("Downloading BIOS update for model $($Model) / product $($ProductCode) to $($DownloadPath)")
  107.     Get-Softpaq -Number $BIOS.Id -SaveAs $BIOS_EXE -Verbose
  108.     Write-Host "Downloading BIOS CVA file to $BIOS_CVA"
  109.     Out-File $LogFile -Append -InputObject ("Downloading BIOS CVA file to $BIOS_CVA")
  110.     Invoke-WebRequest -Uri $BIOS.MetaData -OutFile $BIOS_CVA
  111.     Write-Host "Downloading BIOS HTML file to $BIOS_HTML"
  112.     Out-File $LogFile -Append -InputObject ("Downloading BIOS HTML file to $BIOS_HTML")
  113.     Invoke-WebRequest -Uri $BIOS.ReleaseNotes -OutFile $BIOS_HTML
  114.     Write-Host "Writing README file with BIOS info to $BIOS_TXT"
  115.     Out-File $LogFile -Append -InputObject ("Writing README file with BIOS info to $BIOS_TXT")
  116.     $BIOS | Out-File -FilePath $BIOS_TXT
  117.  
  118.     #Extract BIOS files
  119.     Write-Host "Extracting downloaded BIOS file to $($ExtractPath)"
  120.     Out-File $LogFile -Append -InputObject ("Extracting downloaded BIOS file to $($ExtractPath)")
  121.     Start-Process $BIOS_EXE -ArgumentList "-pdf -e -s -f$($ExtractPath)" -Wait
  122.  
  123.     #Suspend Bitlocker
  124.     If ((Get-BitLockerVolume -MountPoint "$ENV:SystemDrive").VolumeStatus -eq "FullyDecrypted") {
  125.         Write-Host "Bitlocker not enabled"
  126.         Out-File $LogFile -Append -InputObject ("Bitlocker not enabled")
  127.     }
  128.     Else {
  129.         Write-Host "Suspending Bitlocker"
  130.         Out-File $LogFile -Append -InputObject ("Suspending Bitlocker")
  131.         Suspend-BitLocker -MountPoint "$ENV:SystemDrive" -RebootCount 1
  132.     }
  133.  
  134.     #Search CVA to find path of executable that should be used to install the new BIOS version
  135.     Write-Host "Looking for executable that should be used to install the new BIOS version in CVA file"
  136.     Out-File $LogFile -Append -InputObject ("Looking for executable that should be used to install the new BIOS version in CVA file")
  137.     $CVA_Install_Command = Get-Content $BIOS_CVA | Where { $_ -like "*SilentInstall*" }
  138.     $CVA_Install_EXE = $CVA_Install_Command | % { $_.Split('"')[1] }
  139.     Write-Host "Install command from CVA found: $CVA_Install_EXE"
  140.     Out-File $LogFile -Append -InputObject ("Install command from CVA found: $CVA_Install_EXE")
  141.  
  142.     #Run update command with password file support
  143.     $Updater_EXE = "$ExtractPath\$CVA_Install_EXE"
  144.     $BIOS_PW = "$ScriptDir\Password\HP_BIOS_PW.bin"
  145.     If (Get-HPBiosSetupPasswordIsSet) {
  146.         Write-Host "BIOS password protection detected"
  147.         Out-File $LogFile -Append -InputObject ("BIOS password protection detected")
  148.         If (!(Test-Path $BIOS_PW)) {
  149.             Write-Host "HP BIOS password file missing: $BIOS_PW not found" -ForegroundColor Yellow
  150.             Out-File $LogFile -Append -InputObject ("HP BIOS password file missing: $BIOS_PW not found")
  151.         }
  152.         Write-Host "Using $CVA_Install_EXE to Flash BIOS with arguments -s -r -b -l -p"
  153.         Out-File $LogFile -Append -InputObject ("Using $CVA_Install_EXE to Flash BIOS with Args -s -r -b -l -p")
  154.  
  155.         #Flash BIOS
  156.         Write-Host "Command = $Updater_EXE -s -r -b -l$($SaveToRoot)\HPBIOSUpdate.log -p$($BIOS_PW)"
  157.         Out-File $LogFile -Append -InputObject ("Command = $Updater_EXE -s -r -b -l$($SaveToRoot)\HPBIOSUpdate.log -p$($BIOS_PW)")
  158.         Start-Process $Updater_EXE -ArgumentList "-s -r -b -l$($SaveToRoot)\HPBIOSUpdate.log -p$($BIOS_PW)" -Wait
  159.     }
  160.     Else {
  161.         Write-Host "BIOS password protection not detected"
  162.         Write-Host "Using $CVA_Install_EXE to Flash BIOS with Args -s -r -b -l"
  163.         Out-File $LogFile -Append -InputObject ("BIOS password protection not detected")
  164.         Out-File $LogFile -Append -InputObject ("Using $CVA_Install_EXE to Flash BIOS with arguments -s -r -b -l")
  165.  
  166.         #Flash BIOS
  167.         Write-Host "Command = $Updater_EXE -s -r -b -l$($SaveToRoot)\HPBIOSUpdate.log"
  168.         Out-File $LogFile -Append -InputObject ("Command = $Updater_EXE -s -r -b -l$($SaveToRoot)\HPBIOSUpdate.log)")
  169.         Start-Process $Updater_EXE -ArgumentList "-s -r -b -l$($SaveToRoot)\HPBIOSUpdate.log" -Wait
  170.     }
  171.  
  172.     #Cleanup update files
  173.     # Write-Host "Removing folder $SaveToLocation to cleanup install files"
  174.     # Out-File $LogFile -Append -InputObject ("Removing folder $SaveToLocation to cleanup install files")
  175.     # Remove-Item -Recurse -Force $SaveToLocation -ErrorAction SilentlyContinue
  176.  
  177.     #Notify user
  178.     Write-Host "HP BIOS update applied, will install after next reboot" -ForegroundColor Green
  179.     Out-File $LogFile -Append -InputObject ("HP BIOS update applied, will install after next reboot")
  180.     Sleep 5
  181. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement