Name macOS device using Department and last 8 of serial number

ndeangelis
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?

5 REPLIES 5

mm2270
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.

#!/bin/bash

apiUser="$4"
apiPass="$5"
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
    newName="${deptName}${serNumLast8}"
    /usr/local/bin/jamf setComputerName -name "$newName"
else
    echo "Problem. Could not get either last 8 of Serial Number or Department name"
    exit 1
fi

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

ndeangelis
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.

mister2
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`

ndeangelis
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.

mm2270
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 com.apple.smb.server 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/com.apple.smb.server NetBIOSName "$newName"