Jump to content

Mac Laptop Battery Condition


Larry Benshoof

Recommended Posts

Josh Levitsky
This post was recognized by Josh Levitsky!

Larry Benshoof was awarded the badge 'Helpful' and 5 points.

We use this command in a shell script in a custom field to get the battery condition of our Mac laptops.

system_profiler SPPowerDataType | grep "Condition" | awk '{print $2}'

We then have a Query that looks for those Macs that have a battery condition of Service so we can try and replace the battery before the user reports it totally dead. Also nice to know how many batteries to have on hand in our parts inventory.

  • Like 2
Link to comment
Share on other sites

Josh Levitsky
This post was recognized by Josh Levitsky!

AnEngelsen was awarded the badge 'Helpful' and 5 points.

@Larry Benshoof very nice!

This script will tell you the max battery capacity:

All credit goes to FileWave! (But I can't remember who.) 😛

MacOS (Shell)

#!/bin/bash
# -2 => No battery present
# Any non-negative number => max % of original capacity battery can charge to

temp=`ioreg -brc AppleSmartBattery`
if [ -z "$temp" ]; then
	echo "-2"
else
	MaxCapacity=`ioreg -brc AppleSmartBattery | grep \"MaxCapacity\" | cut -d "=" -f 2 | sed 's/ //g'`
	DesignCapacity=`ioreg -brc AppleSmartBattery | grep '"DesignCapacity" =' | cut -d "=" -f 2 | sed 's/ //g'`
	MaxChargeCapacity=`echo "scale=2 ; $MaxCapacity / $DesignCapacity" | bc`
	echo "($MaxChargeCapacity * 100) / 1" | bc
fi

exit 0

Windows (PowerShell)

# Possible values
# -1 => Battery info unavailable
# -2 => No battery present
# Any non-negative number => max % of original capacity battery can charge to
 
if (Get-WmiObject -Class "MSBatteryClass" -Namespace "ROOT\WMI"){
    $DesignCapacity=Get-WmiObject -Class "MSBatteryClass" -Namespace "ROOT\WMI" | select -ExpandProperty DesignedCapacity
} else {
    Write-output "-2" #No battery present
break
}

$FullChargedCapacity=Get-WmiObject -Class "BatteryFullChargedCapacity" -Namespace "ROOT\WMI" | select -ExpandProperty FullChargedCapacity
 
if ($FullChargedCapacity -eq 0 -or $DesignCapacity -eq 0){
    Write-output "-1" #Battery info unavailable
    break
} else
{
    $BatteryCapacity = ($FullChargedCapacity / $DesignCapacity) * 100
    if ($BatteryCapacity -gt 100) {$BatteryCapacity = 100}
 
    $BatteryCapacity = [decimal]::round($BatteryCapacity)
    $BatteryCapacity
}

exit 0 

 

Edited by AnEngelsen
  • Like 1
Link to comment
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
×
×
  • Create New...