PowerShell Check Power Adapter Connected Upgrade UEFI/BIOS – SCCM Application

The other month I was asked by one of my clients to deploy a BIOS/UEFI upgrade to the HP Elite x2 1012 G1. Now this as we all know is fine and very normal, the only issue I had was ensuring that the Power adapter was connected before running the BIOS/UEFI upgrade. This is because.. when you use the force switch on the HP upgrade utility it does not check for the Power Adapter state and also bypasses low battery and will install regardless.

So I created a script….

This script is designed to be run with the BIOS/UEFI upgrade package within.

  1. It will check first the Hardware type of the device (2 being Laptop, 1 being Desktop) making sure it is a mobile device.
  2. The script will then check the WMI property “PowerOnLine” to establish whether or not the power adapter is connected.
  3. If the Power Adapter is connected it will continue with the BIOS/UEFI upgrade (Jump to step 5)
  4. If the Power Adapter is not connected  it will warn the user 10 times before closing and informing the User to Re-Run the application from Software Center when power is available. (Warning count is customizable)
  5. The script will then identify the BIOS/UEFI version and if it is less that the version being deployed it will then warn and install the new BIOS/UEFI version.
  6. The script also does error checking around the Model of the machine.

Anyway enough already… Here is the script. As always feel free to use it but don’t forget where you got it from!

###########################################################################################################################
#Author:        SCCMOG - Richie Schuster - SCCMOG.COM                                                                     #
#ModdedBy:      Your Name here                                                                                            #
#Date:          20/05/2017                                                                                                #
#Name:          Check-PowerAdapter.ps1                                                                                    #
#ConfigMgr/CMD: Powershell.exe -Executionpolicy Bypass -File Check-PowerAdapterUpgradeUEFI.ps1                            #    
#Description:   This script will check if the power adapter has been connected to a system before it continues. It will   #
#           :   also check hardware type and has been configured to only run on laptops. This can be run as an application#
#           :   set to available deployment and visable to user. All comments as always throughout the script explain each#
#           :   step in detail. This was used to upgrade a BIOS/UEFI.                                                     #
###########################################################################################################################

#Amount of times to warn user before exiting.
$maxRepeat = 10
#Your Organisation for the prompts
$Organisation = "SCCMOG"
#New BIOS/UEFI version to deploy.
$NewBiosVersion = "N85 Ver. 01.19"
#WMI Model name of machines to upgrade
$UpgradeModel = "HP Elite x2 1012 G1"


#Add windows forms assembly
Add-Type -AssemblyName System.Windows.Forms | Out-Null

#Check hardware is mobile type
$hardwaretype = Get-WmiObject -Class Win32_ComputerSystem -Property PCSystemType
If ($($hardwaretype.PCSystemType) -ne 1) { 
    #Get Power State
    [BOOL]$Power = (Get-WmiObject -Class BatteryStatus -Namespace root\wmi -ComputerName $env:COMPUTERNAME).PowerOnLine
    #If power not connected warn user and re check until it is or the $MaxRepeat has exceeded
    If ($Power -ne $true) {
        #Keep checking power until one of the until states is true
        do 
        {
            #Reduces MAx repeat by 1
            $maxRepeat--
            #Create Message Box
            $ReturnPower = [System.Windows.Forms.MessageBox]::Show("Please connect the power adapter to continue! $maxRepeat tries remaining!", $Organisation,
            [System.Windows.Forms.MessageBoxButtons]::OKCancel,
            [System.Windows.Forms.MessageBoxIcon]::Warning)
            #If user clicks ok check power state again
            If ($ReturnPower -imatch "OK") {
            [BOOL]$Power = (Get-WmiObject -Class BatteryStatus -Namespace root\wmi -ComputerName $env:COMPUTERNAME).PowerOnLine
            }
        }
        until ($Power -eq $true -or $maxRepeat -eq 0 -or $ReturnPower -imatch "Cancel")
        #If Max repeat hits 0 inform user that they did not comply so the application is exiting
        If ($maxRepeat -eq 0) {
            [System.Windows.Forms.MessageBox]::Show("You didnt connect the power adapter! Please re run from Software Center when power is avalable. Exiting...", $Organisation,
            [System.Windows.Forms.MessageBoxButtons]::OK,
            [System.Windows.Forms.MessageBoxIcon]::ERROR)  
            #Write-Host "You didnt connect power! Please re run from Software Center when power is avalable. Exiting..." -ForegroundColor "Magenta"
            Exit 0
            }
        #If User presses cancel inform user that they did not comply so the application is exiting
        Elseif ($ReturnPower -imatch "Cancel"){
            Write-Host "You Canceled the BIOS/UEFI upgrade please re run when power is avalable." -foregroundcolor "green"
            [System.Windows.Forms.MessageBox]::Show("You Canceled the BIOS/UEFI upgrade please re run from Software Center when power is avalable.", $Organisation,
            [System.Windows.Forms.MessageBoxButtons]::OK,
            [System.Windows.Forms.MessageBoxIcon]::Warning)
            Exit 0
            }
        #If the power is connected then continue
        Elseif ($Power -eq $true){
            [System.Windows.Forms.MessageBox]::Show("Thank you for connecting the power to the device. Please wait for the BIOS/UEFI upgrade to complete until removing the power.", $Organisation,
            [System.Windows.Forms.MessageBoxButtons]::OK,
            [System.Windows.Forms.MessageBoxIcon]::Warning)  
        }
        #Shouldnt get here...
        Else {
            [System.Windows.Forms.MessageBox]::Show("Something went wrong. Please contact the service desk to have the situation corrected.", $Organisation,
            [System.Windows.Forms.MessageBoxButtons]::OK,
            [System.Windows.Forms.MessageBoxIcon]::Error)  
            Exit 0
        }
    }
    #If power is already connected continue....
    Else {
        [System.Windows.Forms.MessageBox]::Show("Succesfully found power adapter. Please wait for the BIOS/UEFI upgrade to complete until removing the power. This usually takes 5 minutes.", $Organisation,
        [System.Windows.Forms.MessageBoxButtons]::OK,
        [System.Windows.Forms.MessageBoxIcon]::Warning)      
    }
}
#If machine is not mobile prompt user.
Else {
    [System.Windows.Forms.MessageBox]::Show("This is a Desktop or Server Machine. Please contact the service desk to have the situation corrected.", $Organisation,
    [System.Windows.Forms.MessageBoxButtons]::OK,
    [System.Windows.Forms.MessageBoxIcon]::Error)
    Exit 0  
}
#Finally launch the BIOS/UEFI Executable....
If ($Power -eq $true) {
        #Current Model
        $Model = Get-WmiObject Win32_ComputerSystem | Select-Object Model
        $Model = $Model.Model
        #If model matches continue
        If ($Model -imatch $UpgradeModel) {
            #Get BIOS/UEFI version
            $BIOS = Get-WmiObject Win32_bios | Select-Object SMBIOSBIOSVersion
            $CurrentBiosVersion = $BIOS.SMBIOSBIOSVersion
                #If current version is less than new BIOS/UEFI version continue.
                If ($CurrentBiosVersion -lt $NewBiosVersion) {
                    #Write-Host "$CurrentBiosVersion is less than $NewBiosVersion. Proceeding to upgrade BIOS" -ForegroundColor "Yellow"
                    #Warn User reboot about to occur and give them the option to cancel
                    $Return = [System.Windows.Forms.MessageBox]::Show("The Machine will now reboot to allow the BIOS/UEFI of this machine to upgrade. Please click OK to continue once all you work is saved.", $Organisation,
                    [System.Windows.Forms.MessageBoxButtons]::OKCancel,
                    [System.Windows.Forms.MessageBoxIcon]::Warning)
                        #If they clicked on continue with the upgrade.
                        If ($Return -imatch "OK") {
                        Write-Host "User Clicked OK! Startin BIOS/UEFI upgrade!"
                        #Start-Process "$PSScriptRoot\HPBIOSUPDREC64.exe" -ArgumentList "-a -s -f dp0N85_0119.bin -a -h -b"
                        }
                        #If they clicked cancel warn and exit
                        Elseif ($Return -imatch "Cancel") {
                        #Write-Host "User Clicked Cancel! Stopping BIOS/UEFI upgrade!"
                        [System.Windows.Forms.MessageBox]::Show("The BIOS/UEFI Upgrade has been stopped please re-run when convenient.", $Organisation,
                        [System.Windows.Forms.MessageBoxButtons]::OK,
                        [System.Windows.Forms.MessageBoxIcon]::Information)
                        Exit 0
                        }
                }
                #If the BIOS/UEFI version is eq to the current then quit.
                Elseif ($CurrentBiosVersion -eq $NewBiosVersion) {
                    [System.Windows.Forms.MessageBox]::Show("The BIOS/UEFI Upgrade has been stopped! You machine's UEFI/BIOS version $CurrentBiosVersion is equal to $NewBiosVersion. Please click OK to quit.", $Organisation,
                    [System.Windows.Forms.MessageBoxButtons]::OK,
                    [System.Windows.Forms.MessageBoxIcon]::Information)
                    Exit 0
                }
                #If the current BIOS/UEFI is gt the new BIOS/UEFI version quit and warn
                Elseif ($CurrentBiosVersion -gt $NewBiosVersion) {
                    [System.Windows.Forms.MessageBox]::Show("The BIOS/UEFI Upgrade has been stopped! You machine's UEFI/BIOS version $CurrentBiosVersion is greater than $NewBiosVersion, no need to upgrade. Please click OK to quit.", $Organisation,
                    [System.Windows.Forms.MessageBoxButtons]::OK,
                    [System.Windows.Forms.MessageBoxIcon]::Information)
                    Exit 0                    
                }
        }
        #Wrong model for Upgrade.
        Else {
            [System.Windows.Forms.MessageBox]::Show("The BIOS/UEFI Upgrade has been stopped! You machine is the incorrect model for this upgrade: $Model", $Organisation,
            [System.Windows.Forms.MessageBoxButtons]::OK,
            [System.Windows.Forms.MessageBoxIcon]::Error)
            Exit 0
        }
}
###############################################################################################################

Leave a Reply

Your email address will not be published. Required fields are marked *

Copyright 2016 SCCMOG | All Rights Reserved

css.php