Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Function Get-ScriptDirectory {
- If ($PSISE) {
- Split-Path $PSISE.CurrentFile.FullPath
- }
- Else {
- $Global:PSScriptRoot
- }
- }
- ##################################
- #Log file info
- $Today = Get-Date -Format yyyy-MM-dd
- $Time = Get-Date -Format HH:mm:ss
- $LogDir = "$ENV:SystemDrive\Windows\Temp"
- $LogFileName = "LOG_HPBiosUpdate.log"
- $LogFile = Join-Path $LogDir $LogFileName
- #Start logging
- Out-File $LogFile -Append -InputObject ("")
- Out-File $LogFile -Append -InputObject ("")
- Out-File $LogFile -Append -InputObject ("$Today - $Time")
- Out-File $LogFile -Append -InputObject ("")
- #Define locations
- $ScriptDir = Get-ScriptDirectory
- $SaveToRoot = "$ENV:SystemDrive\Windows\Temp"
- $SaveToDirectory = "HP_BIOS"
- $SaveToLocation = "$SaveToRoot\$SaveToDirectory"
- $DownloadDir = "$($SaveToLocation)\HP_BIOS_Downloads"
- $ExtractDir = "$($SaveToLocation)\HP_BIOS_Extract"
- If (!(Test-Path $SaveToLocation)) {
- $null = New-Item -Path $SaveToRoot\ -Name $SaveToDirectory -ItemType "Directory" -Force
- }
- If (!(Test-Path $DownloadDir)) {
- $null = New-Item -Path $SaveToLocation\ -Name "HP_BIOS_Downloads" -ItemType "Directory" -Force
- }
- If (!(Test-Path $ExtractDir)) {
- $null = New-Item -Path $SaveToLocation\ -Name "HP_BIOS_Extract" -ItemType "Directory" -Force
- }
- #Define variables
- $OS = "Win10"
- $Category = "BIOS"
- $ProductCode = (Get-WmiObject -Class Win32_BaseBoard).Product
- $Model = (Get-WmiObject -Class Win32_ComputerSystem).Model
- $HP_CMSL_URL = "https://ftp.hp.com/pub/caps-softpaq/cmit/release/cmsl/hp-cmsl-latest.exe"
- #Detect HP BIOS PS module
- Try {
- Get-HPBiosVersion | Out-Null
- Write-Host "HP Client Management Script Library already installed"
- Out-File $LogFile -Append -InputObject ("HP Client Management Script Library already installed")
- }
- Catch {
- Write-Host "HP Client Management Script Library not loaded"
- Write-Host "Downloading from $HP_CMSL_URL"
- Out-File $LogFile -Append -InputObject ("HP Client Management Script Library not loaded")
- Out-File $LogFile -Append -InputObject ("Downloading from $HP_CMSL_URL")
- Invoke-WebRequest -Uri $HP_CMSL_URL -OutFile "$($DownloadDir)\HP_CMSL.exe"
- Write-Host "Installing HP Client Management Script Library from $($DownloadDir)\HP_CMSL.exe"
- Out-File $LogFile -Append -InputObject ("Installing HP Client Management Script Library from $($DownloadDir)\HP_CMSL.exe")
- Start-Process -FilePath "$($DownloadDir)\HP_CMSL.exe" -ArgumentList "/Silent /NoCancel /NoRestart /CloseApplications /Log" -Wait
- Write-Host "Finished downloading and installing HP Client Management Script Library"
- Out-File $LogFile -Append -InputObject ("Finished downloading and installing HP Client Management Script Library")
- }
- #Get BIOS info
- $CurrentBIOS = Get-HPBiosVersion
- Write-Host "Currently installed BIOS: version $($CurrentBIOS)"
- Write-Host "Checking product code $($ProductCode) for BIOS updates"
- Out-File $LogFile -Append -InputObject ("Currently installed BIOS: version $($CurrentBIOS)")
- Out-File $LogFile -Append -InputObject ("Checking product code $($ProductCode) for BIOS updates")
- $BIOS = Get-SoftpaqList -Platform $ProductCode -Os $OS -Category $Category
- $MostRecent = ($BIOS | Measure-Object -Property "ReleaseDate" -Maximum).Maximum
- $BIOS = $BIOS | Where "ReleaseDate" -eq "$MostRecent"
- If ([VERSION]$CurrentBIOS -ge [VERSION]$BIOS.Version) {
- Write-Host "BIOS already current"
- Out-File $LogFile -Append -InputObject ("BIOS already current")
- }
- Else {
- Write-Host "Updated BIOS available: version $([VERSION]$BIOS.Version)"
- Out-File $LogFile -Append -InputObject ("Updated BIOS available: version $([VERSION]$BIOS.Version)")
- #Create folders
- $DownloadPath = "$($DownloadDir)\$($Model)\$($BIOS.Version)"
- $ExtractPath = "$($ExtractDir)\$($Model)\$($BIOS.Version)"
- If (!(Test-Path $DownloadPath)) {
- $null = New-Item $DownloadPath -ItemType Directory -Force
- }
- If (!(Test-Path $ExtractPath)) {
- $null = New-Item $ExtractPath -ItemType Directory -Force
- }
- #Download BIOS files
- $BIOS_CVA = "$($DownloadPath)\$($BIOS.Id).cva"
- $BIOS_HTML = "$($DownloadPath)\$($BIOS.Id).html"
- $BIOS_EXE = "$($DownloadPath)\$($BIOS.Id).exe"
- $BIOS_TXT = "$($DownloadPath)\$($BIOS.ReleaseDate).txt"
- Write-Host "Downloading BIOS update for model $($Model) / product $($ProductCode) to $($DownloadPath)"
- Out-File $LogFile -Append -InputObject ("Downloading BIOS update for model $($Model) / product $($ProductCode) to $($DownloadPath)")
- Get-Softpaq -Number $BIOS.Id -SaveAs $BIOS_EXE -Verbose
- Write-Host "Downloading BIOS CVA file to $BIOS_CVA"
- Out-File $LogFile -Append -InputObject ("Downloading BIOS CVA file to $BIOS_CVA")
- Invoke-WebRequest -Uri $BIOS.MetaData -OutFile $BIOS_CVA
- Write-Host "Downloading BIOS HTML file to $BIOS_HTML"
- Out-File $LogFile -Append -InputObject ("Downloading BIOS HTML file to $BIOS_HTML")
- Invoke-WebRequest -Uri $BIOS.ReleaseNotes -OutFile $BIOS_HTML
- Write-Host "Writing README file with BIOS info to $BIOS_TXT"
- Out-File $LogFile -Append -InputObject ("Writing README file with BIOS info to $BIOS_TXT")
- $BIOS | Out-File -FilePath $BIOS_TXT
- #Extract BIOS files
- Write-Host "Extracting downloaded BIOS file to $($ExtractPath)"
- Out-File $LogFile -Append -InputObject ("Extracting downloaded BIOS file to $($ExtractPath)")
- Start-Process $BIOS_EXE -ArgumentList "-pdf -e -s -f$($ExtractPath)" -Wait
- #Suspend Bitlocker
- If ((Get-BitLockerVolume -MountPoint "$ENV:SystemDrive").VolumeStatus -eq "FullyDecrypted") {
- Write-Host "Bitlocker not enabled"
- Out-File $LogFile -Append -InputObject ("Bitlocker not enabled")
- }
- Else {
- Write-Host "Suspending Bitlocker"
- Out-File $LogFile -Append -InputObject ("Suspending Bitlocker")
- Suspend-BitLocker -MountPoint "$ENV:SystemDrive" -RebootCount 1
- }
- #Search CVA to find path of executable that should be used to install the new BIOS version
- Write-Host "Looking for executable that should be used to install the new BIOS version in CVA file"
- Out-File $LogFile -Append -InputObject ("Looking for executable that should be used to install the new BIOS version in CVA file")
- $CVA_Install_Command = Get-Content $BIOS_CVA | Where { $_ -like "*SilentInstall*" }
- $CVA_Install_EXE = $CVA_Install_Command | % { $_.Split('"')[1] }
- Write-Host "Install command from CVA found: $CVA_Install_EXE"
- Out-File $LogFile -Append -InputObject ("Install command from CVA found: $CVA_Install_EXE")
- #Run update command with password file support
- $Updater_EXE = "$ExtractPath\$CVA_Install_EXE"
- $BIOS_PW = "$ScriptDir\Password\HP_BIOS_PW.bin"
- If (Get-HPBiosSetupPasswordIsSet) {
- Write-Host "BIOS password protection detected"
- Out-File $LogFile -Append -InputObject ("BIOS password protection detected")
- If (!(Test-Path $BIOS_PW)) {
- Write-Host "HP BIOS password file missing: $BIOS_PW not found" -ForegroundColor Yellow
- Out-File $LogFile -Append -InputObject ("HP BIOS password file missing: $BIOS_PW not found")
- }
- Write-Host "Using $CVA_Install_EXE to Flash BIOS with arguments -s -r -b -l -p"
- Out-File $LogFile -Append -InputObject ("Using $CVA_Install_EXE to Flash BIOS with Args -s -r -b -l -p")
- #Flash BIOS
- Write-Host "Command = $Updater_EXE -s -r -b -l$($SaveToRoot)\HPBIOSUpdate.log -p$($BIOS_PW)"
- Out-File $LogFile -Append -InputObject ("Command = $Updater_EXE -s -r -b -l$($SaveToRoot)\HPBIOSUpdate.log -p$($BIOS_PW)")
- Start-Process $Updater_EXE -ArgumentList "-s -r -b -l$($SaveToRoot)\HPBIOSUpdate.log -p$($BIOS_PW)" -Wait
- }
- Else {
- Write-Host "BIOS password protection not detected"
- Write-Host "Using $CVA_Install_EXE to Flash BIOS with Args -s -r -b -l"
- Out-File $LogFile -Append -InputObject ("BIOS password protection not detected")
- Out-File $LogFile -Append -InputObject ("Using $CVA_Install_EXE to Flash BIOS with arguments -s -r -b -l")
- #Flash BIOS
- Write-Host "Command = $Updater_EXE -s -r -b -l$($SaveToRoot)\HPBIOSUpdate.log"
- Out-File $LogFile -Append -InputObject ("Command = $Updater_EXE -s -r -b -l$($SaveToRoot)\HPBIOSUpdate.log)")
- Start-Process $Updater_EXE -ArgumentList "-s -r -b -l$($SaveToRoot)\HPBIOSUpdate.log" -Wait
- }
- #Cleanup update files
- # Write-Host "Removing folder $SaveToLocation to cleanup install files"
- # Out-File $LogFile -Append -InputObject ("Removing folder $SaveToLocation to cleanup install files")
- # Remove-Item -Recurse -Force $SaveToLocation -ErrorAction SilentlyContinue
- #Notify user
- Write-Host "HP BIOS update applied, will install after next reboot" -ForegroundColor Green
- Out-File $LogFile -Append -InputObject ("HP BIOS update applied, will install after next reboot")
- Sleep 5
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement