Battery Health Manager Discovery Script

This blog post will cover how to inventory the battery health manager setting for a collection of devices leveraging a PowerShell script to generate a .csv file.

There will be various options listed in this blog. One option will cover an automation process for Microsoft Endpoint Configuration Manager (previously known as SCCM), where a task sequence will be created and deployed out to the selected device collections to return the battery health manager setting on the devices using an additional script. The latter option will be for non-Configuration Manager environments that can utilize the Battery Health Manager script without the Configuration Manager integration.

The battery health manager (BHM) value on the devices will be returned in a .csv file and this information can be leveraged to ensure the value is set to what is recommended by HP, this will help maintain the health of battery on HP devices. To view more details on the battery health manager and how to deploy it in your environment refer to an earlier blog published by Dan Felman that covers more details and background on the topic. HP Developers Portal | Improving Battery Health with Battery Health Manager (Upd 10/12/2020)

Let’s get into setting up the environment that will be leveraged for Microsoft Configuration Manager.

Note: If you do not use Configuration Manager scroll down to the 'Script to Inventory Battery health Manager Setting' section, that is independent of Configuration Manager.

Automating Task Sequence Creation

In the first part we will be discussing the script that will help generate the task sequence, deploy it to a collection and lastly it will call out the Battery Health Manager script that takes an inventory of the BHM setting.

The first step in this process will be to ensure that the configuration manager cmdlet is present in your PowerShell environment. To ensure this you can follow the steps as follows:

On the left top corner of the Configuration Manager console select the white downward triangle icon and choose the “Connect via Windows PowerShell ISE option”.

This will open up Windows PowerShell ISE and load up an auto-generated script. Run this script to configure the site and import the ConfigurationManager.psd1 module.

This is what the script should look like, with the difference in $SiteCode and $ProviderMachineName variable values:

# Site configuration

$SiteCode = "PS1" # Site code

$ProviderMachineName = "CM01.corp.viamonstra.com" # SMS Provider machine name



# Customizations

$initParams = @{}

#$initParams.Add("Verbose", $true) # Uncomment this line to enable verbose logging

#$initParams.Add("ErrorAction", "Stop") # Uncomment this line to stop the script on any errors



# Do not change anything below this line



# Import the ConfigurationManager.psd1 module

if((Get-Module ConfigurationManager) -eq $null) {

    Import-Module "$($ENV:SMS_ADMIN_UI_PATH)\..\ConfigurationManager.psd1" @initParams

}

After this script runs successfully, we can now start with implementing our script to create the task sequence. This script leverages the ConfigurationManager.psd1 module and thus is essential for this process. The $Script and $Collection variables will need to be assigned for your environement. 

<#
.Name
HPBatteryHealthManagerDiscovery.ps1

.Synopsis
Script to create a task sequence in Config Manager and return "HP Battery Health Manager" setting

.DESCRIPTION
This script is used to return the "HP Battery Health Manager" settings for HP Notebooks and Mobile Workstations by creating
a Task Sequence and deploying to a Device Collection in Microsoft Configuration Manager

.Notes
Created: 10/25/2021
Version: 0.1
Authors : Casey Monroe, Meghna Gupta, Quick Reddy, Dan Felman
Disclaimer: This script is provided "AS IS" with no warranties, confers no rights and is not supported by the authors.

.Dependencies
None

.Version History
0.1 10/25/2021
Initial Release

#>

#Script Variables
$Script = "<BHM Script address>"
$Collection = "<Device Collection>"

#Declaration of the Task Sequence variables
$TSBHMName = "Battery Health Manager"
$TSBHMModule_Desc = "This task calls the Battery Health Manager script to run and export a .csv file to your set designation"

#Creating the task sequence
$ModuleCheck = Get-CMTaskSequence -Name $TSBHMName
if ( $ModuleCheck ) {
$ReturnTSAvailability = $true
$CustomName = Read-Host -Prompt "This Task Sequence already exists, enter a new Task Sequence name"
$BHMModuleCustom = New-CMTaskSequence -CustomTaskSequence -Name $CustomName -Description $TSBHMModule_Desc
$BHMGroupCustom = New-CMTaskSequenceGroup -Name "Battery Health Manager"
Add-CMTaskSequenceStep -InsertStepStartIndex 0 -TaskSequenceName $CustomName -Step $BHMGroupCustom

#Command to run the Battery Health Manager Script
$BHMCommand = "Powershell.exe -ExecutionPolicy ByPass -File $Script"
$BHMCommand_Desc = "Run BHM Discovery script to determine the BHM settings"
$BHMCmdSubStep = New-CMTSStepRunCommandLine -CommandLine $BHMCommand -Name "Battery Health Manager Discovery" -Description $BHMCommand_Desc

#Configuring the Task Sequence Step to run Battery Health Manager
Set-CMTaskSequenceGroup -InputObject $BHMModuleCustom -StepName $BHMGroupCustom.Name -AddStep $BHMCmdSubStep -InsertStepStartIndex 1
} else {
$BHMModule = New-CMTaskSequence -CustomTaskSequence -Name $TSBHMName -Description $TSBHMModule_Desc
$BHMGroup = New-CMTaskSequenceGroup -Name "Battery Health Manager"
Add-CMTaskSequenceStep -InsertStepStartIndex 0 -TaskSequenceName $TSBHMName -Step $BHMGroup

#Command to run the Battery Health Manager Script
$BHMCommand = "Powershell.exe -ExecutionPolicy ByPass -File $Script"
$BHMCommand_Desc = "Run BHM Discovery script to determine the BHM settings"
$BHMCmdSubStep = New-CMTSStepRunCommandLine -CommandLine $BHMCommand -Name "Battery Health Manager Discovery" -Description $BHMCommand_Desc


#Configuring the Task Sequence Step to run Battery Health Manager
Set-CMTaskSequenceGroup -InputObject $BHMModule -StepName $BHMGroup.Name -AddStep $BHMCmdSubStep -InsertStepStartIndex 1
}
#Creating Deployment process
New-CMTaskSequenceDeployment -InputObject $BHMModule -Availability Clients -CollectionName $Collection -Comment "Battery Health Manager Discovery" -DeployPurpose Required -RerunBehavior AlwaysRerunProgram -ScheduleEvent AsSoonAsPossible -SendWakeupPacket $True -ShowTaskSequenceProgress $True

Note: This script will attempt to create a task sequence called "Battery Health Manager". If it already exists, it will ask for a new name to be entered.

Remember to change the variables to meet your environment. This is everything that is needed for the first part of this blog, that is to create a task sequence and deploy it to the appropriate device collection.

Now let’s discuss the Battery Health Manager script that returns the value of BHM from the device and appends it to a .csv file.

Script to Inventory Battery Health Manager Setting

Here are the scripts that will export the Battery Health Manager settings into a .csv file. You will see two different versions below, one that uses our Client Management Script Library and one that uses WMI without the need of Client Management Script Library. If you are following the “Automating Task Sequence Creation” portion above, one of these scripts will be used and added to the $Script variable.

Note: These scripts are independent of Microsoft Endpoint Configuration Manager and can be leveraged in other environments as well. 

CMSL -

<#
.Name
HPBatteryHealthManagerCheck_HPCMSL.ps1

.Synopsis
Script to return "HP Battery Health Manager" setting

.DESCRIPTION
This script is used to return the "HP Battery Health Manager" setting for HP Notebooks and Mobile Workstations

.Notes
Created: 9/29/2021
Version: 0.3
Authors : Casey Monroe, Meghna Gupta, Quick Reddy, Bob Stern
Disclaimer: This script is provided "AS IS" with no warranties, confers no rights and is not supported by the authors.

.Dependencies
None

.Version History
0.3 10/5/2021
Added "Not Applicable" for instances where Battery Health Manager doesn't exist.
0.2 10/5/2021
Added CSV Path Variable
Fixed Script Name
0.1 9/29/2021
Initial Release

#>

# Script Variables
$BHMSettingName = "Battery Health Manager"
$SerialNumberSetting = "Serial Number"
$csvPath = "<csv file output Path>"
#$csvPath = "$ENV:TEMP\test.csv"

try {
$BHMSetting = Get-HPBIOSSettingValue -Name $BHMSettingName
}
catch [System.Management.Automation.ItemNotFoundException] {
"Setting not found: 'Battery Health Manager'"
$BHMSetting = "Not applicable"
}
finally {
$SerialNumber = Get-HPDeviceSerialNumber
$ComputerName = $env:COMPUTERNAME
$csvOutput = New-Object PSObject -Property @{ Serial = $SerialNumber ; BHMSetting = $BHMSetting; ComputerName = $ComputerName; }
}
if ($csvOutput) {Export-Csv -InputObject $csvOutput -Path $csvPath -NoTypeInformation -Append }
Write-Output $csvOutput

WMI -

Note: If you are running this WMI script on a notebook, the if ($computerModel.Model.Contains('Book')) value will remain unchanged. If this is being run on a Desktop, it can be adjusted to "Desk", and "One" for All-In-One. For an X2 platform the value will need to be updated to 'x2'

<#
.Name
HPBatteryHealthManagerCheck_noHPCMSL.ps1

.Synopsis
Script to return "HP Battery Health Manager" setting

.DESCRIPTION
This script is used to return the "HP Battery Health Manager" setting for HP Notebooks and Mobile Workstations

.Notes
Created: 9/29/2021
Version: 0.3
Authors : Casey Monroe, Meghna Gupta, Quick Reddy, Bob Stern
Disclaimer: This script is provided "AS IS" with no warranties, confers no rights and is not supported by the authors.

.Dependencies
None

.Version History
0.3 10/5/2021
Changed to account for earlier systems with three Battery Health Manager options
Added "Not Applicable" for instances where Battery Health Manager doesn't exist.
0.2 10/5/2021
Added CSV Path Variable
0.1 9/29/2021
Initial Release

#>

# Get BIOS Setting Function

Function Get-BIOSSetting
{
param(
[parameter(Mandatory=$true, HelpMessage="Provide BIOS Setting Name to get BIOS Setting Value returned")]
[ValidateNotNullOrEmpty()]
[string]$Setting
)
$BIOS = Get-WmiObject -class hp_biossetting -Namespace "root\hp\instrumentedbios"
$BIOSSetting = $BIOS | Where-Object {$_.Name -eq $Setting}
Return $BIOSSetting
}

# Script Variables
$BHMSettingName = "Battery Health Manager"
$SerialNumberSetting = "Serial Number"
$csvPath = "<csv file output Path>"
#$csvPath = "$env:TEMP\test.csv"
$computerModel = Get-WmiObject -class Win32_ComputerSystem

if ($computerModel.Manufacturer -like 'HP') {
if ($computerModel.Model.Contains('Book')) {
try {
$BHMSetting = (Get-BIOSSetting -Setting $BHMSettingName).CurrentValue
}
catch [System.Management.Automation.ItemNotFoundException] {
"Setting not found: 'Battery Health Manager'"
$BHMSetting = "Not applicable"
}
finally {
$SerialNumber = Get-BIOSSetting -Setting $SerialNumberSetting
$ComputerName = $env:COMPUTERNAME
$csvOutput = New-Object PSObject -Property @{ Serial = $SerialNumber.Value ; BHMSetting = $BHMSetting; ComputerName = $ComputerName; }
}
}
}
if ($csvOutput) {Export-Csv -InputObject $csvOutput -Path $csvPath -NoTypeInformation -Append }
Write-Output $csvOutput

These scripts should now help you inventory the BHM setting for your entire fleet. Please feel free to reach out with any comments or questions.

 

Authored by Casey Monroe and Meghna Gupta

Author : meghna.gupta