How to Rename Macs Automatically With Jamf

I wanted to create a better naming scheme than only serial number or generic “Mati’s Mac”. More convenient is to rename computers with their owner’s and computer model’s name. Why? Because when you’re looking reports from your deployments or what ever, you can tell straight away what you are dealing with.

We are using Jamf’s API interface for user inquiries so we can determine who is the owner of this computer!

First you need to setup a “service account” to your Jamf instance so create new standard account and for security reasons strip its privileges to minimum. I think it needs only reading permissions to Users and Computers, nothing else.

Then you need to force users to tell who they are during the setup process. I kindly ask them to authenticate using Jamf’s built-in PreStage Enrollments “Require Authentication” setting. The Require Authentication checkbox is only displayed if an LDAP server has been set up in Jamf Pro, more on this on Jamf’s guide: https://docs.jamf.com/10.25.0/jamf-pro/administrator-guide/Computer_PreStage_Enrollments.html

Then is the fun part, deploying the script. I scoped following script for computers which have completed the specific PreStage where owners name is asked. Just to be sure user information is available on Jamf instance. Script is triggered to be executed with “Enrollment Complete” trigger. You need to provide information to first three variables for this to work. First is the name you gave for the service account, then password. jssHost variable for your instance’s address.

#!/bin/sh

jssUser=userName
jssPass=password
jssHost=https://companyname.jamfcloud.com

serial=$(ioreg -rd1 -c IOPlatformExpertDevice | awk -F'"' '/IOPlatformSerialNumber/{print $4}')
model="$(system_profiler SPHardwareDataType|grep "Model Name")"
username=$(/usr/bin/curl -H "Accept: text/xml" -sfku "${jssUser}:${jssPass}" "${jssHost}/JSSResource/computers/serialnumber/${serial}/subset/location" | xmllint --format - 2>/dev/null | awk -F'>|<' '/<username>/{print $3}')

if [ "$username" == "" ]; then
    echo "Error: Username field is blank."
    exit 1

elif [ "$model" == "      Model Name: iMac" ]
then
        scutil --set ComputerName "imac-$username"
        scutil --set HostName "imac-$username"
        scutil --set LocalHostName "imac-$username"
        
elif [ "$model" == "      Model Name: MacBook Pro" ]
then
        scutil --set ComputerName "mbp-$username"
        scutil --set HostName "mbp-$username"
        scutil --set LocalHostName "mbp-$username"
        
elif [ "$model" == "      Model Name: MacBook Air" ]
then
        scutil --set ComputerName "mba-$username"
        scutil --set HostName "mba-$username"
        scutil --set LocalHostName "mba-$username"
        
else
        scutil --set ComputerName "macmini-$username"
        scutil --set HostName "macmini-$username"
        scutil --set LocalHostName "macmini-$username"
fi

If there is no username available, script will exit and do nothing. Otherwise it will determine what Mac model is it working with, who does it belong to and rename the device accordingly.

And voilà, renaming automated.

Leave a Reply

Your email address will not be published. Required fields are marked *