Worklet: Battery Health Check for Windows Laptops

  • 7 October 2020
  • 1 reply
  • 39 views

Userlevel 5

This worklet will determine the health percentage of a battery in a Windows laptop. There are two ways to use it…


The first way is if you want to set it on a schedule (don’t run this manually or you will get erroneous information from skipping the evaluation). This will automatically generate entries to the activity log for only laptops with a health lower than what is set as an acceptable percentage. The machines will also appear as non-compliant for the worklet until the issue is resolved.


Evaluation:


# Set Minimum Acceptable Health Percentage.
$MinHealth = "90"

# Determine type of computer
$HardwareType = (Get-WmiObject -Class Win32_ComputerSystem).PCSystemType

# Checking Battery Specification.
$BatteryDesignSpec = (Get-WmiObject -Class "BatteryStaticData" -Namespace "ROOT\WMI").DesignedCapacity
$BatteryFullCharge = (Get-WmiObject -Class "BatteryFullChargedCapacity" -Namespace "ROOT\WMI").FullChargedCapacity

# Check if Battery Replacement is required.
[int]$CurrentHealth = ($BatteryFullCharge/$BatteryDesignSpec) * 100

if ($CurrentHealth -lt $MinHealth -and $HardwareType -eq 2) {
Exit 1
}
else
{
Exit 0
}

Remediation:


# Checking Battery Specification.
$BatteryDesignSpec = (Get-WmiObject -Class "BatteryStaticData" -Namespace "ROOT\WMI").DesignedCapacity
$BatteryFullCharge = (Get-WmiObject -Class "BatteryFullChargedCapacity" -Namespace "ROOT\WMI").FullChargedCapacity

# Check if Battery Replacement is required.
[int]$CurrentHealth = ($BatteryFullCharge/$BatteryDesignSpec) * 100
Write-Output "Battery needs to be replaced $CurrentHealth% !"

The second way is if you want to only manually run it against one or more groups. It will put an entry in the activity log for every Windows laptop giving the health percentage along with whether it is healthy or needs to be replaced based on what you set the acceptable health percentage at. Since it runs against all Windows devices in this manner, there will be a blank activity log entry for non-laptop devices.


Evaluation:


Exit 0

Remediation:


# Set Minimum Acceptable Health Percentage.
$MinHealth = "90"

# Determine type of computer
$HardwareType = (Get-WmiObject -Class Win32_ComputerSystem).PCSystemType

# Checking Battery Specification.
$BatteryDesignSpec = (Get-WmiObject -Class "BatteryStaticData" -Namespace "ROOT\WMI").DesignedCapacity
$BatteryFullCharge = (Get-WmiObject -Class "BatteryFullChargedCapacity" -Namespace "ROOT\WMI").FullChargedCapacity

# Check if Battery Replacement is required.
[int]$CurrentHealth = ($BatteryFullCharge/$BatteryDesignSpec) * 100
if ($CurrentHealth -lt $MinHealth -and $HardwareType -eq 2) {write-output "Battery needs to be replaced $CurrentHealth% !"}
if ($CurrentHealth -ge $MinHealth -and $HardwareType -eq 2) {write-output "Battery is healthy $CurrentHealth%"}

1 reply

Is it possible to update the repository with your corrected code? The repository has the incorrect codes instead of Get_WmiObject it has Get_CIMInstance. I am sure this confuses a lot of new users when they download from within their portal.

Reply