Name Computer Based on JSS Username + Mac Model

mstensond49
New Contributor II

Hello I have succefully implimented the following script.

Name computer using JSS username

I would like to also pull the Model of the computer from the JSS for the computer name as well so it looks something like this.

"jsmith MacBook Air (13-inch Mid 2013)"

Thank you so much for this!
What do I need to add? Any thoughts @mottertektura

7 REPLIES 7

mm2270
Legendary Contributor III

Are you sure you want to name them that way? Those human readable model names can sometimes be long. Combined with the username, your computer names will be pretty long. It might be better to consider just using the username + model identifier if possible.

If you really are set on doing this, I can provide code that grabs the full model name from Apple's servers using a portion of the serial number. You can combine that with the username, which you can get a few different ways, and then name the systems accordingly.

mstensond49
New Contributor II

Can't it just pull from the Model inventory attribute in the JSS? The length of the name shouldn't be a problem unless we are looking to bind to AD which we have no plans to now or ever. Can you think of any other reason this would be a problem? Thanks

StoneMagnet
Contributor III

At least through JSS 9.97 the description in the JSS inventory hasn't been 100% accurate, hence the need for @mm2270's approach of querying Apple.

mm2270
Legendary Contributor III

Yes, as @StoneMagnet mentioned, unfortunately due to Apple re-using model identifiers for slightly different models over the course of the last 4 or so years, and the JSS using a static table lookup, the names you see in the JSS are not always accurate. Depending on your fleet of Macs, there could be as many as 10% or more systems that are showing up with the wrong full model name.

OTOH, if you don't care so much about that, yes, it's possible to get this info from the API. Here is code that would grab the full model name

#!/bin/sh

UUID=$(ioreg -rd1 -c IOPlatformExpertDevice | awk -F'"' '/IOPlatformUUID/{print $4}')

fullModelName=$(curl -H "Accept: text/xml" -sfku apiusername:apipassword "https://your.jss.url:8443/JSSResource/computers/udid/$UUID/subset/hardware" | xpath /computer/hardware/model[1] | awk -F'>|<' '/<model>/{print $3}')

echo "$fullModelName"

Replace strings where needed in the above. It should pull and print out the full model name as your JSS sees it.

mstensond49
New Contributor II

@mm2270 TRhank you. Where would those lines fit in the original script?

mm2270
Legendary Contributor III

@mstensond49 I assume by 'original script', you mean the one from @mottertektura located here?

If so, this is the original script, reposted

#!/bin/sh

jssUser=$4
jssPass=$5
jssHost=$6

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

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

else

/usr/sbin/scutil --set HostName "$username"
/usr/sbin/scutil --set LocalHostName "$username"
/usr/sbin/scutil --set ComputerName "$username"

fi

So, given that you want to add the computer model information to the name, you would change it to look like this

#!/bin/sh

jssUser=$4
jssPass=$5
jssHost=$6

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

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}')

modelName=$(/usr/bin/curl -H "Accept: text/xml" -sfku "${jssUser}:${jssPass}" "${jssHost}/JSSResource/computers/serialnumber/${serial}/subset/hardware" | xpath /computer/hardware/model[1] | awk -F'>|<' '/<model>/{print $3}')

## If either the username or model name came back blank from the above API calls, exit
if [[ "$username" == "" ]] || [[ "$modelName" == "" ]]; then
    echo "Error: The Username or Model Name field is blank."
    exit 1
else
    fullCompName="${username} ${modelName}"

    /usr/sbin/scutil --set HostName "$fullCompName"
    /usr/sbin/scutil --set LocalHostName "$fullCompName"
    /usr/sbin/scutil --set ComputerName "$fullCompName"
fi

Again, I don't think this is a great idea. Your computer names will end up with spaces and other characters in them that could cause issues under some circumstances. In particular, the LocalHostName and HostName may end up truncating the information or changing it in some way. But it's your call.

mstensond49
New Contributor II

@mm2270 Thank you! I appriciate your concern but I have to ask what is different than the system naming the computer "John's Macbook Air" upon Setup Assistant than me naming it "jsmith MacBook Air (13-inch Mid 2013)". Yes there are parentheses, but in both naming schemes there are spaces and special characters. Other than that what would be the problem? I will be doing extensive testing on multiple test systems before this goes live of course. Thanks so much.