Rename iPads with API

AHolmdahl
New Contributor III

Purpose: I want to name the iPads to the users real name.
I have "Enforce Mobile Device Names" checked in prestage. I run an API script which changes the iPads name in JAMF. The mobile command is sent to the iPad and the name is changed locally on the iPad. So far so good...

but.. At the next inventory update the iPads name reverts to the standard name defined by Pre-stage. Is this really expected behaviour? I thought that the name set in JAMF was the master name.

Any ideas, grateful for all/any help.

21 REPLIES 21

RLR
Valued Contributor

Not had to change iPad names much in Jamf but I have found that if you have a config profile installed that restricts the user from changing the name of their device then it reverts back on the next inventory update.

amoscaritola
New Contributor III

Try it without enforcing the name in the pre-stage, maybe that's overriding your changes. When you change the name using the api it will automatically enforce the mobile name and check the "Enforce Mobile Device Name" checkbox on each device.

/JSSResource/mobiledevicecommands/command/DeviceName/<device name>/id/<device jss id>

MrRoboto
Contributor III

Can anyone share a script to batch rename iPads from a CSV?

ryan_ball
Valued Contributor

@MrRoboto I believe you can do this with MUT:
https://jssmut.weebly.com/

MrRoboto
Contributor III

@ryan.ball Thanks. Previously I was using MUT but it does not check the Enforce Device Name Box. Set device name is okay but box not checked so it won't be enforced. Only checked if previously checked from a past manual rename or prestage enrollment naming setting.

jbisgett
Contributor II

There is an open PI regarding this issue:
PI-009042
When updating a device name using the Jamf Pro API or the Classic API, the Enforce Mobile Device Name setting is not automatically enabled.

Ecco_Luke
Contributor II

Has any progress been made on PI-009042? It's so annoying having to rename devices manually, especially since Pro doesn't offer you to set the Name in the Inventory Preload yet School does!

PorkChopExpress
New Contributor II
New Contributor II

PI-009042 should be resolved in 10.33

Hopefully so; is there a source that corroborates that?

palmna
Contributor

@AHolmdahl, Would you mind sharing your script?  I've been performing name updates weekly but I'm manually exporting the CSV, then using MUT to process a new CSV where i've copied "Real Name" into the "Mobile Device Name".  I would love to automate this.

nadsad
New Contributor III

Did AHolmdahl by any chance share the script with anyone in this conversation?

We would love to get a hold of it.

Not me.  Unfortunately, I'm still exporting a list of wrongly named devices and correcting them with MUT on a weekly basis.  

nadsad
New Contributor III

Thats unfortunate :(

If anybody else has something similar it would be much appreciated if you could share :)

VeV
New Contributor II

    Just came across this post and wanted to share our method for setting mobile device names in response to @palmna and @nadsad. There are a few pieces here:

 

    1. Create a smart group that contains the devices that require renaming. For @palmna  the smart group criteria might  be something like "username" is not "".

 

    2. Get the JSS ID of the smart group. You can get this from your browser when viewing the Smart Group. It is found in the URL:

 

     For example the following Smart Group ID would be "103":

     https://your.jssurl.com/smartMobileDeviceGroups.html?id=103&o=r

 

    3. In this case, we also want to get the devices assigned username. We can also do this from the API by downloading the xml and parsing it for "username".

 

    4. We will also need to get the Device ID using the same method as above. We will use this to post the new device name to the JSS.

 

    5. Now that all the information has been gathered, loop through the serial numbers of each device and set the name.

 

    Below is a modified version of the script I use. You can taylor this to meet your own needs. You can run it directly using something like CodeRunner (This is what I usually use) or, for a fully automated workflow, use a launch daemon to run it automatically at a set interval. Here you go,  Hope this helps!

 

#!/bin/zsh

# Created by Vasean Everett

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Define Variables

jssURL="https://YOUR.JSS.URL.com/"
jssUser="<YOURAPIUSERNAME>"
jssPass="<YOURAPIPASSWORD>"

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Define functions

getXML() {
# Get the serial numbers of all devices in "Incorrect Hostname" smart group (ID: 103)
endpoint="JSSResource/mobiledevicegroups/id/103"
xml=$(curl -X GET -u ${jssUser}:${jssPass} ${jssURL}${endpoint} -H "accept: application/xml")
devices=($(printf "$xml" | xmllint --format - | awk -F'>|<' '/<serial_number>/{print $3}' | sort -n))
}

getDeviceInfo() {
# API call to retrieve the device inventory information
# Parse inventory information for the username, device id and serial number of the device
endpoint="JSSResource/mobiledevices/serialnumber/"
xml=$(curl -su ${jssUser}:${jssPass} -H "accept: text/xml" ${jssURL}${endpoint}${serial} -X GET)
username=$(printf "${xml}" | xmllint --xpath '/mobile_device/location/username/text()' - 2>/dev/null)
deviceID=$(printf "${xml}" | xmllint --xpath '/mobile_device/general/id/text()' - 2>/dev/null)
}

setMobileDeviceName() {
# API call to set mobile device name
endpoint="JSSResource/mobiledevicecommands/command/DeviceName/${username}/id/${deviceID}"
curl -su ${jssUser}:${jssPass} -H "accept: text/xml" ${jssURL}${endpoint} -X POST 1>/dev/null
}

fixiPadHostname() {
getXML
for serial in ${devices[@]}
do
getDeviceInfo
setMobileDeviceName
printf "Set device \"$serial\" name to \"$username\"\n"
done
}

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# main

fixiPadHostname

exit 0

 

 

 

 

 

 

 

@VeV

This is fantastic!  I wish I had checked back on this thread much sooner. I spent weeks trying to solve this, and it looks like I came close to your proposed solution.  I am hitting a snag, though...when I plug in all of my info to your script and run it, my results are:

Set device "ABCDEF123456" name to ""
Set device "ABCDEF123457" name to ""
Set device "ABCDEF123458" name to ""
Set device "ABCDEF123459" name to ""
Set device "ABCDEF123460" name to ""

 I can't get the script to return a value for the 'username' variable. 

After days of massaging this script, I was finally able to get it working.  The only issue now is that the names don't stick.  Has anyone else run into this issue when using a script to rename devices?

KD6-3DOT7
New Contributor II

One thing to check is to navigate to the inventory record for one of the devices, and make sure "Enforce Mobile Device Name" is un-checked.

 

Enfore Mobile Device Name.PNG

RLR
Valued Contributor

Have you got a config profile pushed out to these devices with the restriction: Modyfying device name: Restricted?

I've had issues with this in the past.

KD6-3DOT7
New Contributor II

@palmna ,

My apologies for the delay in response. I've moved organizations since posting here so I was no longer getting notifications. I just happened across it again today. Based on the output, it would appear that the inventory records for these devices do not have a value in the username field. I would check the inventory information for one of the serial numbers to see.

One option too, would be to comment out line 27 (In the example given above), the line that assigns a value to "username" i.e. 

username=$(printf "${xml}" | xmllint --xpath '/mobile_device/location/username/text()' - 2>/dev/null)

 and replace it with:

username=$(printf "${xml}" | xmllint --xpath '/mobile_device/location/username/text()' -)

then run the script again, you may see something like the following: 

-:10: parser error : Opening and ending tag mismatch: br line 8 and p
</p>
    ^
-:11: parser error : Opening and ending tag mismatch: p line 8 and body
</body>
       ^
-:12: parser error : Opening and ending tag mismatch: body line 5 and html
</html>
       ^
-:12: parser error : Premature end of data in tag html line 1
</html>

 In this case, it would indicate the variable "username" had no value.

 

Hope this helps!

palmna
Contributor

In case anyone else is still wrestling with his in September 2023 or beyond....

After giving up on this and then revisiting it months later, it turns out that the names weren't sticking because I was using the classic API (which is hot garbage in this use case).  Once I updated my script to utilize the new Jamf API, everything worked like a charm.  iPad names are now persistent through inventory updates, and I no longer see 300+ incorrectly named iPads at the end of every day.

Hi Palmna,

That sounds awesome! What endpoint did you use to achieve this in Jamf Pro API?

I tried /JSSResource/mobiledevices/serialnumber/$number in Classic API, but the hostname only changed in Jamf System and did not sync to real devices. 

Thanks!