Problems setting the device name on an Apple TV via script/API

brentac
New Contributor II

Before I explain the problem I should probably mention I'm very new to scripting. I'll happily accept "operator error" if it leads to an answer.

I have a script that I wrote that sets the Mobile Device Name of an Apple TV via the Classic API. It's run via a policy in Self Service. It prompts for a Serial Number, the name you want to set it too, url encodes the name, and then sets the name.

I've started noticing that after the Apple TV does an Update Inventory, the name will revert to whatever it was previously. That's usually a serial number when the Apple TV is first configured, or if it's something that we had set through the jamf website it'll revert to that as well.

We use a dedicated Jamf account for this, but I've even tried the script with my own account which is a full admin account. The script is using basic authentication but I tried it with a bearer token as well with the same result. I've also tried running it via Self Service or via the terminal directly.

I don't know if it's relevant but we are using Jamf Pro in a Jamf Cloud instance. I feel like there is something obvious I'm missing.

Here is a sample of the actual command being sent. It's really part of a larger script but with this sample I'm able to produce the same results of a name not taking. 

#!/bin/bash

#Prefill the URL/Username/Password or for some reason I end up with a parser error.

jamfURL=
Username=
Password=
serialNumber=
newName=

if [ -z $jamfURL ]; then
	echo "Please enter the Jamf Pro URL (with no slash at the end):"
	read -r jamfURL
fi 

if [ -z $Username ]; then
	echo "Please enter your Jamf Pro username:"
	read -r jamfUsername
fi 

if [ -z $Password ]; then 
	echo "Please enter the Jamf Pro password for account: $jamfUsername:"
	read -r -s jamfPassword
fi

if [ -z $serialNumber ]; then 
	echo "Please enter the Serial Number of the Apple TV"
	read -r serialNumber
fi

if [ -z $newName ]; then 
	echo "Please enter the new name of the Apple TV"
	read -r newName
fi


encodedPassword=$(printf $Username:$Password | base64)

encodedName=$(printf %s "$newName"| /usr/local/nkc/jq -sRr @uri)

# get Apple TV information
deviceInfo=$(curl -X GET "$jamfURL/JSSResource/mobiledevices/serialnumber/$serialNumber" -H "accept: application/json" -H "Authorization: Basic $encodedPassword")

idNumber=$(echo "$deviceInfo" | jq -r ".mobile_device.general.id")

# Change the name of the Apple TV
curl -X POST "$jamfURL/JSSResource/mobiledevicecommands/command/DeviceName/$encodedName/id/$idNumber" -H "accept: application/json" -H "Authorization: Basic $encodedPassword"

After sending the command I can see the name change on an Apple TV I just cant figure out why it isn't holding. Any clues as to what I'm missing would be greatly appreciated. 

4 REPLIES 4

brentac
New Contributor II

Well I'll answer my own question. I tried the new API and that one seems to hold the name changes even after an Update Inventory. So I'll incorporate that.

IT-Chris
New Contributor III

can you share what you did?

brentac
New Contributor II

This is the specific command we are using for naming Apple TVs. the ID number variable is the JSS ID.

nameCommand=$(curl --request PATCH \
			 --url $jamfURL/api/v2/mobile-devices/$idNumber \
			 --header "Authorization: Bearer $bearerToken" \
			 --header 'accept: application/json' \
			 --header 'content-type: application/json' \
			 --data '{ "name": "'"$newName"'" }' \
			 --silent)

burnhamt
New Contributor III

I use the MUT Application. It a great tool for doing bulk import and name changes. 

MUT on the Mac App Store (apple.com)