JAMF : Rename a mac to lastname-serial

I wanted to standardize the naming convention of macs to something unique but also easy to find when the user is on the line. This script runs in my DEPNotify chain upon enrollment.

After a lot of thinking , lastname-serial was the best option to :

1 – Keep name short (firstname.lastname-serial was nice but could end up sometimes in more than 35+ chars)

2 – Make it identifiable by using our user’s attribute

3 – Make it unique, even if some people could have same lastname by using serial number

#!/bin/sh

#fetch logged in console user
loggedInUser=$( scutil <<< "show State:/Users/ConsoleUser" | awk '/Name :/ && ! /loginwindow/ { print $3 }' )



echo "Logged in User Name : $loggedInUser"


#trim principle: if no . the full string is used as fallback
#  ##   <-- greedy front trim
#  *    <-- matches anything
#  .    <-- until the last '.'


lastname=${loggedInUser##*.}


echo "Lastname : $lastname"

serial="$(ioreg -l | grep IOPlatformSerialNumber | sed -e 's/.*\"\(.*\)\"/\1/')"

Echo "serial number : $serial"

#Creates the correct computer name places it in a variable
computerName="$lastname-$serial"

echo "Renaming to : $computerName"

sudo jamf setComputerName -name "$computerName"
sudo jamf recon

Leave a comment