I've Got The Power

brockwalters
Contributor II

I've Got The Power 

An Extension Attribute script for Mac portable battery health:

 

 

#!/bin/bash
#shellcheck disable=SC2207

if ! /usr/sbin/sysctl -n hw.model | /usr/bin/grep -q -i 'book'
then
	echo "<result>no</result>"; exit
fi

IFS=$'\n'
pwrdata=($(/usr/libexec/PlistBuddy \
-c "print 0:_items:0:sppower_battery_health_info:sppower_battery_health" \
-c "print 0:_items:0:sppower_battery_health_info:sppower_battery_cycle_count" \
-c "print 0:_items:0:sppower_battery_health_info:sppower_battery_health_maximum_capacity" \
-c "print 0:_items:0:sppower_battery_charge_info:sppower_battery_max_capacity" \
-c "print 0:_items:0:sppower_battery_model_info:sppower_battery_manufacturer" \
-c "print 0:_items:0:sppower_battery_model_info:sppower_battery_firmware_version" \
-c "print 0:_items:0:sppower_battery_model_info:sppower_battery_hardware_revision" \
-c "print 0:_items:0:sppower_battery_model_info:sppower_battery_cell_revision" \
/dev/stdin <<< "$(/usr/sbin/system_profiler -xml SPPowerDataType)" 2> /dev/null)) 

result(){
echo "Health = ${pwrdata[0]}"
echo "Cycle Count = ${pwrdata[1]}"
if [[ "${pwrdata[2]}" == *'%'* ]]
then
	echo "Max Capacity = ${pwrdata[2]}"
else
	echo "Max Capacity = ${pwrdata[2]} mAh"
fi
if [ "$(/usr/sbin/sysctl -in hw.optional.arm64)" = 1 ] && /usr/sbin/sysctl -n machdep.cpu.brand_string | /usr/bin/grep -q 'Apple' && /usr/bin/uname -v | /usr/bin/grep -q 'ARM64'
then
	i=5 #  Silicon				
else
	i=6     # Intel
	echo "Manufacturer = ${pwrdata[i-3]}" 
fi
echo "Firmware Version = ${pwrdata[i-2]}"
echo "Hardware Revision = ${pwrdata[i-1]}"
echo "Cell Revision = ${pwrdata[i]}"
}
echo "<result>$(result)</result>"

 

 

Issues:

  1. I want to separate Mac portables (or notebooks, or laptops, or whatever you like to call them...) from non-portables without need for a Smart Group. That's what the 1st section does. Every Mac portable since the beginning of time has had "Book" in the name. This is how I like to find them. You may like another way.
  2. Intel Mac portables & Apple Silicon Mac portables have different battery reporting! This sucks & I found out only after version 1 of this EA.
  3. Not everyone is aware that you can pass multiple arguments to a single PlistBuddy command (although zsh sometimes is a little crybaby about how much data can be parsed in 1 stream if you add too many...)
  4. I wanted the data to appear in a multi-line format. I am a multi-line Extension Attribute fan. Always have been, always will be. When some version of Jamf broke my multi-line EAs a few years back, I died a little inside. I also wanted customized, human-readable labels for the attributes. This craziness with the conditionals at the end handles the labels, order & the cases (Intel vs. Apple Silicon.)

Here is the result in Jamf. See how one has mAh & the other has % a different number of lines?

Screen Shot 2022-03-15 at 2.43.59 PM.png

Screen Shot 2022-03-15 at 2.42.12 PM.png

Anyway, that's all. I know there are fancier ways to do this. OSQuery, Fleet, etc. I like EAs.

PS. Update:

Fleet (which uses OSQuery) was not reporting this correctly. :) I believe they have addressed this based on my EA (fun!) 

I am reporting this in Feedback Assistant - I don't understand why  stopped reporting battery manufacturer...

4 REPLIES 4

Anonymous
Not applicable

I made two very simple script for this. One only shows the batteries health status and the second shows the loading cycles.

I made two extension attributes for the values, shown at the hardware section of the client.

Here are the scripts:

Loading cycles:

Expand

#!/bin/bash

BatteryCycles=$(system_profiler SPPowerDataType | grep "Cycle Count" | awk '{print $3}')

echo "<result>$BatteryCycles</result>"

health status:

Expand

#!/bin/bash

BatteryStatus=$(system_profiler SPPowerDataType | grep "Condition" | awk '{print $2}')

echo "<result>$BatteryStatus</result>"

With these parameters, I hope to be able to remark, if one of our Macs will be needed to be brought to the Apple Service Program for exchange the battery. The idea is, to create a smart computer group in affect to the parameters, that will warn me to bring in the Macs to the Apple Service Program if the battery is no longer healthy and the Service Program is still available.

Maybe it will be helpfully for someone.

Welp, 1) you never need grep to awk:

 

system_profiler SPPowerDataType | awk '/Condition/{print $2}'
Normal

 

You just need awk.


2) You can also use my version for smart groups as well. Works great:

Screen Shot 2022-03-18 at 9.21.12 AM.png

You could build a smart group on any of the attributes listed, i.e., a 1:many extension attribute from which any number of smart groups can be built instead of 1 extension attribute per smart group.

Anonymous
Not applicable

that's much easier, tnx a lot for this hint. I am at the beginning in scripting on mac :)

 

qmoreland
New Contributor

Wow.  I have been looking for exactly this for a while. Thank you so much for posting this!