How to Check Mac Battery Condition using Intune

In this blog post, I will show you how to check Mac battery condition using Intune. Microsoft Intune collect many attributes about an enrolled device by default like Device name, enrolled by, Serial number, Primary user information and more. However, it does not fetch the battery condition of Mac devices.

You can easily query battery condition information from your managed macOS devices and pull the data into intune using Custom attributes for macOS feature. I have written a detailed step-by-step guide on creating custom attributes for macOS using Intune. In the next steps, I will show you the script, its deployment process and where to get this data on Intune portal.

Checking Mac Battery Condition Manually

You can check the battery condition of your Mac device by clicking on the Apple menu > System settings. Click on Battery in the sidebar and check the battery health. There are different status depending upon the battery health. You may see one of below two states of the battery condition on a Mac device.

  • Normal: Battery is functioning normally.
  • Service recommended: Battery holds less charge than when new or is not functioning normally. You can continue to use your Mac, but your battery needs service.

Prerequisites

  • macOS devices are enrolled in Intune and running macOS 12 or later.
  • Devices must be able to reach the internet directly (proxy isn’t supported for the scripting agent).

Step 1: Download getBatteryCondition.sh Shell script

Microsoft has already provided pre-built custom attribute shell scripts which are ready to deploy. Download the script from Microsoft GitHub repo: getBatteryCondition.sh. Below are the script contents. We will use this script in the next step.

Download getBatteryCondition.sh Shell script

getBatteryCondition.sh

#!/bin/bash
#set -x

############################################################################################
##
## Extension Attribute script to return the macOS Battery Condition
##
############################################################################################

## Copyright (c) 2020 Microsoft Corp. All rights reserved.
## Scripts are not supported under any Microsoft standard support program or service. The scripts are provided AS IS without warranty of any kind.
## Microsoft disclaims all implied warranties including, without limitation, any implied warranties of merchantability or of fitness for a
## particular purpose. The entire risk arising out of the use or performance of the scripts and documentation remains with you. In no event shall
## Microsoft, its authors, or anyone else involved in the creation, production, or delivery of the scripts be liable for any damages whatsoever
## (including, without limitation, damages for loss of business profits, business interruption, loss of business information, or other pecuniary
## loss) arising out of the use of or inability to use the sample scripts or documentation, even if Microsoft has been advised of the possibility
## of such damages.
## Feedback: [email protected]

# Check if system profiler output contains battery information
if system_profiler SPPowerDataType | grep -q "Battery Information"; then
    # Extract and print battery condition
    condition=$(system_profiler SPPowerDataType | awk -F': ' '/Condition:/ {print $2}')
    echo "${condition:-Unknown}"
else
    echo "No Battery"
fi

Step 2: Deploy getBatteryCondition.sh Script

Next, we will deploy getBatteryCondition.sh script using Intune admin center. Let’s check the steps:

  • Sign in to the Intune admin center > Devices > macOS > Custom attributes for macOS > click Create.
Deploy Custom Attribute shell script Intune
  • Basics tab: Provide a Name and Description of the policy. For example: Check Battery Condition.
  • Attribute settings:
    • Data type of attribute: There are three values in the drop-down String, Integer, and Date. Select the value to match with the output of your script. Our Battery condition shell script returns String value as output. Therefore, we will select String.
    • Script: Browse the script file using the blue folder icon and click Next.
Deploy getBatteryCondition.sh Script
  • Scope tags (optional): This is an optional configuration, skip it by clicking on Next. A scope tag in Intune is an RBAC label you add to resources (policies, apps, devices) to limit which admins can see and manage them.
  • Assignments: Create or use an entra security group containing macOS users or devices. Click on Add groups and select the group to assign this policy.
  • Review + Add: Review the deployment summary and Click on Add.

That’s it, Custom attributes for macOS script is created. You can view all your Custom attributes scripts under Devices > macOS > Custom attributes for macOS.

Sync Intune Policies

The device check-in process might not begin immediately. If you’re testing this policy on a test device, you can manually kickstart Intune sync from the device itself or remotely through the Intune admin center.

Alternatively, you can use PowerShell to force the Intune sync on Windows devices. Restarting the device is another way to trigger the Intune device check-in process.

Monitor Mac Battery Condition Script Status

  • Go to Devices > macOS > Custom attributes for macOS.
  • From the Overview page, check if the status is Succeeded or there is an Error.
Monitor Custom Attribute Script Status

Battery Condition Script Result

You can check and confirm the return value of Custom attributes script from Intune admin center. In our scenario, we deployed a script to check battery condition of macOS computers. Let’s check how to find the result.

  • Sign in to the Intune admin center > Devices > macOS > Custom attributes for macOS.
  • Click on your Intune deployment (Check Battery Condition) and go to Device status. Check under the Result column, which will show the script return value.

Below screenshot shows the Result as No Battery as it’s a virtual machine. For physical Mac device, it will show the actual value like Normal or Service recommended.

Mac Battery condition result on Intune

Troubleshooting

For troubleshooting custom attribute script related issues, you can read the log files to investigate. Refer to IntuneMDMDaemon*.log and IntuneMDMAgent*.log files, which could provide useful information for troubleshooting and finding out the root cause of the issue. For more information about how to locate these files on a macOS device, see my other blog post, Collect Intune Logs from macOS Devices.

Read More

Leave a Comment