03-15-2022 02:23 PM - edited 12-28-2022 09:59 AM
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:
Here is the result in Jamf. See how one has mAh & the other has % a different number of lines?
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...
03-18-2022 05:40 AM - edited 03-18-2022 06:26 AM
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:
#!/bin/bash
BatteryCycles=$(system_profiler SPPowerDataType | grep "Cycle Count" | awk '{print $3}')
echo "<result>$BatteryCycles</result>"
health status:
#!/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.
03-18-2022 08:25 AM - edited 03-18-2022 08:32 AM
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:
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.
Posted on 03-18-2022 09:12 AM
that's much easier, tnx a lot for this hint. I am at the beginning in scripting on mac :)
Posted on 05-20-2022 08:53 AM
Wow. I have been looking for exactly this for a while. Thank you so much for posting this!