Name macOS device using Department and last 8 of serial number

New Contributor III

We would like to rename our macOS devices using the JSS Department ID and the last 8 Digits of the serial number. I created an Extension Attribute to pull the last 8 of the serial for this. How can I accomplish this only using the JSS API? Is this possible?


Legendary Contributor III

You shouldn't need to pull the last 8 into an Extension Attribute if it's only purpose is for a system rename. I would just pull it at the time the script runs to rename the Mac.
Example script below. Use $4 for the API username and $5 for the account password. You could also hardcode those in, but I don't recommend it.
You might need to add some things to the script. For example, I'm not sure if you want to add any characters between the Dept and Serial Number, like a dash for example. If so, add that in where it sets the new computer name.
Also, I forget if the jamf binary sets all names on the computer or just the Sharing name. You might need to run additional commands if you want them all set the same.


jssURL=$(defaults read /Library/Preferences/com.jamfsoftware.jamf.plist jss_url)

serNum=$(ioreg -rd1 -c IOPlatformExpertDevice | awk -F'"' '/IOPlatformSerialNumber/{print $4}')
serNumLast8="${serNum: -8}"

deptName=$(curl -H "Accept: application/xml" -sfku "${apiUser}:${apiPass}" "${jssURL}JSSResource/computers/serialnumber/$serNum/subset/location" | xpath '/computer/location/department/text()')

if [ ! -z "$serNumLast8" ] && [ ! -z "$deptName" ]; then
    /usr/local/bin/jamf setComputerName -name "$newName"
    echo "Problem. Could not get either last 8 of Serial Number or Department name"
    exit 1

Let me know if this doesn't fit your needs and you need any help with modifications.

New Contributor III

@mm2270 This is fantastic! We just moved over from AirWatch and needed a way to name our computers without depending on mounting a network share for a csv file. This is monumental! Thank you so much for this. I am going to try it out tomorrow morning.

New Contributor II

we have this to grab the last 7 of the serial for computer naming purposes

# Set a variable with the last 7 digits of SN
last7sn=`/usr/sbin/system_profiler SPHardwareDataType | awk '/Serial/ { print $NF }' | tail -c 8`

New Contributor III

@mm2270 Thank you so much! I have another question. What about the NetBIOSName? I see that it is coming up as No Name.

Legendary Contributor III

@ndeangelis I'm not sure how necessary it is to set the NetBIOS name, as I thought that got set automatically at some point by the system, but, if you really want to set it, it's stored in the plist. Something like this in your script would set it to the same thing as the computer name.

/usr/bin/defaults write /Library/Preferences/SystemConfiguration/ NetBIOSName "$newName"