Skip to main content
Jamf Nation, hosted by Jamf, is the largest Apple IT management community in the world. Dialog with your fellow IT professionals, gain insight about Apple device deployments, share best practices and bounce ideas off each other. Join the conversation.

Computer Rename Based on JamfPro Object Attributes

First some Basic Background

We are beginning to Use Apple DEP for new MAC's, so when they come, fresh out of the box, the user gets no Options, the MAC immediately looks at DEP and gets pointed at out JamfPro Cloud.

This is working great, we Have a Prestage Enrollment set up and working, so we just Add the new machine to the Scope and all is peachy.

The problem is that as an out of the box machine, it will default to a generic name "iMAC" or "MacbookPro" or "Mac Mini", this is no good to me as we have a strict naming standard to follow. The Prestaging via DEP does not seem allow for assigning a name in advance on a per machine basis, and believe me, although it would be useful to simply get the users to enter the name at a prompt, it's a no go option, and we want to try and make this as automated as possible.

So we are now looking like waiting for the machine to be plugged in, and as soon as it Enrolls we just manually update a field on its object and at next check in it will rename and then proceed to domain join.

My hope was to Have an Extension Attribute set up called "Target Computer Name" and use a Smart Group to compare it to the Computer Name, and if they didn't Match, launch a Script to rename the Machine and then possibly set another attribute to show it had been done so I could use it as a trigger for a Domain Join.

It appears Smart Groups wont allow comparison of 2 Attributes, so I looked at a script to do it.
My First Option was to use the "Target Computer Name" Extension attribute as a Variable for the script, Computer Name is automatically sent as Variable $1 so I was hoping that I could set Variable $4 as the contents of my Extension Attribute, apparently this is a No No as well.

I have been looking instead at a script to pull the "Target Computer Name" Extension Attribute directly using the API, do the rename if required and write a True/False to another Extension Attribute "Computername Matches" and use that as a trigger for my Domain Join next step. However it would appear a little tricky and every post I have found so far all look to do it in slightly differing ways, and none of them work, at least not on the version I am running (JAMF Pro 10.1.1 in the Cloud)

Can anyone offer help on this or possibly even some of the other methods I have already tried and failed with ?

Like Comment
Order by:
SOLVED Posted: 1/11/18 at 9:29 AM by NoahRJ

What's the strict naming convention that you have to follow? Is it something where you can pull information from the machine itself and set the computer name that way (e.g. MAC address) or is it something non-machine specific like a particular location and a numerical designation?

Either way, you can probably do this in a single script to check against the current machine name value by running something like scutil --get ComputerName and if it's not the expected value or pattern the name follows, reset it. The logic will be a little more complex if it's something that can't be pulled from the computer name, but it's doable.

SOLVED Posted: 1/11/18 at 9:44 AM by HNTIT

Sadly It's not based on any information that can be collected.

Its a 4 letter designation based on the region of the world, followed by a 2 letter code based on the device type, then a 3 number code assigned to the country/office and then an ascending 4 digit number basically assigned in the order the device arrives.



EMEA - The Europe, Middle East and Africa Region
MB - Its a MacBook
044 - International Dialing Code for the UK
0112 - the 112'th MacBook in that location

So the only place we can do this is to Manually type the desired number into the Computer Object in JAMF as soon as is appears, and then try to grab it from there.

SOLVED Posted: 1/11/18 at 10:27 AM by NoahRJ

Is there a rhyme or reason to network segments specific to region so you could segment this out based on IP addresses and assign the region and the international dialing code based on identifiable network information? You can pull the hardware information from the machine by running system_profiler SPHardwareDataType, and then maybe query the API to pull down a list of other machines that match the values you've assigned up to that point, find the last one in the set with the highest number, and add one to the value to name this new machine.

SOLVED Posted: 1/11/18 at 10:29 AM by mm2270

Some of that it may be possible to get from the device. Certainly the 2 character device type can be gleaned in a script using ioreg or system_profiler to get the model type and then using a case statement to assign the 2 character device code.

As for both the geo region and the international dialing code, let me ask. Do user accounts get assigned to the Mac that are tied into any directory service, like AD? Or are these strictly local accounts? If it's the former, I wonder if getting user information out of the directory service to see where the user location is can be used in a script to determine the dialing code. Might be complicated to do, but might be feasible.
The last bit sounds difficult. I'm not sure how you would track the iteration of the Mac in a specific location to increment the number. I wonder if you could just use the Jamf Pro computer ID number instead, which will be unique across any Macs enrolled in the console.

SOLVED Posted: 1/11/18 at 10:44 AM by HNTIT

The Naming standard has been in place for over 15 years now, I am not going to be able to change it just for a shortcoming in JAMF I'm afraid.

The information is not going to be able to be Generated by the machine or a combination of machine or AD information we can pull.

I need to be able to Enter a Text String into JAMF somewhere that will be used to rename the MAC, and that needs to be completed before we bind to the AD Domain.

Apologies its such a knotty issue

SOLVED Posted: 1/11/18 at 10:52 AM by mm2270

Ok, so next question. You mentioned setting up or looking at setting up a "Target Computer Name" Extension Attribute. Is that something you are able to do? Are you populating that string today? What problem are you running into when trying to pull that string with the API exactly? There's likely a way to do it, but it may take some work and experimentation to make it work reliably.

SOLVED Posted: 1/11/18 at 12:35 PM by HNTIT

Yes I Have set up the Extension Attribute without issue and I can enter data into it.

Every Script I can find on how to pull this out using the API does not work, most of them seem to have been written a while ago for a previous version, I cant find anything that comes close to working now. :(

SOLVED Posted: 1/11/18 at 12:53 PM by NoahRJ

@HNTIT I just threw this together and it works fine for me on Jamf Pro 10:



serial=$(system_profiler SPHardwareDataType | grep "Serial Number" | awk '{print $4}')

Extension_Attributes=$(curl -s -u ${apiUser}:${apiPass} "${apiURL}${serial}" | xpath '/computer/extension_attributes/extension_attribute' 2>/dev/null)

echo "${Extension_Attributes}"

You can drill deeper with xpath to get the specific value of the extension attribute you're looking for, but replace my values with the ones you'll be using for name, password, FQDN of your Jamf Pro endpoint, swap out with serial number (or whatever you'll be using as an identifier for the machine) if you want to test on a specific machine, and give that a go.

SOLVED Posted: 1/11/18 at 1:33 PM by NoahRJ

Fleshing this out a little more here - you can specify the EA ID (at the EA# field below) in this and it'll return the computer name you've assigned for each one:



serial=$(system_profiler SPHardwareDataType | grep "Serial Number" | awk '{print $4}')

ComputerName=$(/usr/bin/curl -s -u ${apiUser}:${apiPass} "${apiURL}${serial}" | /usr/bin/xpath '/computer/extension_attributes/extension_attribute[id = "EA#"]/value/text()' 2>/dev/null)

echo "${ComputerName}"

#Set the computer name from the returned value above
hostname $ComputerName
scutil --set ComputerName $ComputerName
scutil --set LocalHostName $ComputerName
scutil --set HostName $ComputerName
SOLVED Posted: 1/11/18 at 1:33 PM by mm2270

Not tested here, but can you try something like this? Be sure to plug in your information, like the API username and password. If it works, you can later pass those strings as parameters to the script instead of keeping them hardcoded in.


## Fill in these variables with your own information

## The display name of the Extension Attribute goes here
EAName="<some name goes here>"

## This gets the Mac's serial number
serialNumber=$(ioreg -rd1 -c IOPlatformExpertDevice | awk -F'"' '/IOPlatformSerialNumber/{print $4}')

## This gets the Mac's current name
macName=$(scutil --get ComputerName)

## This attempts to get the EA value using the API and the above variables
assignedName=$(curl -H "Accept: application/xml" -sfku "${apiUser}:${apiPass}" "${jssURL}JSSResource/computers/serialnumber/${serialNumber}/subset/extension_attributes" 2>/dev/null | xmllint --format - | grep -A2 "$EAName" | awk -F'>|<' '/<value>/{print $3}')

## Now compare the EA value with the current computer name
if [ ! -z "$assignedName" ]; then
    if [[ "$macName" != "$assignedName" ]]; then
        ## Rename the Mac to the assigned name
        /usr/local/bin/jamf setComputerName -name "$assignedName"
        exit 0
        echo "Mac name already matches assigned name. Exiting..."
        exit 0
    echo "Could not get assigned name from computer record"
    exit 0

Edit: I was in "long post limbo" and didn't see the ones above from @NoahRJ before hitting the Post It button. Looking at the other responses, I like the way he's getting the EA value using xpath and the EA ID a little better since that's likely more reliable. Maybe combine what he has and the rename section of my script together and it should get you where you need to be.

SOLVED Posted: 1/11/18 at 2:38 PM by HNTIT

Very Odd, it does not work for me, tried copying and pasting it onto a terminal window, the line with the curl in it never completes, just sits there forever.

If I remove the port 8443 from the URL it completes but returns no results.

I'm using an admin, is there some special permissions you need for the API user account ???

SOLVED Posted: 1/12/18 at 7:47 AM by HNTIT

It's Working Now

Thanks ALL

SOLVED Posted: Yesterday at 7:54 PM by ScottyBeach

I need to figure out the same thing. Could you post the final script you settled on?
- Scott