Leveraging API to rename computer in script

New Contributor

Hey Jamf guys and gals!


I am trying to query information from our JAMF Pro server through the API. I'm like halfway there but I definitely don't know what I am doing completely. 


The name scheme we want to do is "(first 4 or 5 characters of the building)-(JAMF ID #)" . I am able to query both of those pieces of information through a script that calls the API, however I can't seem to get the "cut" command to take the first 4 or 5 characters of the building information. Any information on how to cut up the building information would be so helpful. This is what I have so far:




serial=$(/usr/sbin/ioreg -rd1 -c IOPlatformExpertDevice | /usr/bin/awk -F'"' '/IOPlatformSerialNumber/{print $4}')
computerID=$(/usr/bin/curl -u $apiuser:$apipass $jssurl:8443/JSSResource/computers/serialnumber/$serial -H "accept: text/xml" | /usr/bin/xpath -e "/computer/general/id/text()")
location=$(/usr/bin/curl -u $apiuser:$apipass $jssurl:8443/JSSResource/computers/serialnumber/$serial -H "accept: text/xml" | /usr/bin/xpath -e "/computer/location/building/text()")


If I echo the location information, it does spit out the full building name. If I try to use it like this, it doesn't echo any information anymore:

location=$(/usr/bin/curl -u $apiuser:$apipass $jssurl:8443/JSSResource/computers/serialnumber/$serial -H "accept: text/xml" | /usr/bin/xpath -e "/computer/location/building/text()") | cut -b 1-4


At the end once I get the string, I'm sure I'll us scutil to rename the computer and disconfigad to bind to AD, but I need help getting this first part done. 


Thanks for taking the time to read if you did.


New Contributor III
New Contributor III

You're very close. There are a bunch of programs to substring data in scripts (cut/awk/sed/etc...) but parameter expansion is built in so usually the best for simple tasks. 


# Fill in these values

# Get this mac's serial number
serial=$(/usr/sbin/ioreg -rd1 -c IOPlatformExpertDevice | /usr/bin/awk -F'"' '/IOPlatformSerialNumber/{print $4}')

# Pull the computer record from Jamf
computerInfo=$(/usr/bin/curl \
  --user   "${apiCreds}" \
  --header "accept: text/xml" \
  --silent \
  --show-error \
  --url    "${jssurl}/JSSResource/computers/serialnumber/${serial}")

# Extract needed info
building=$(echo "$computerInfo" | /usr/bin/xpath -e "/computer/location/building/text()")
computerID=$(echo "$computerInfo" | /usr/bin/xpath -e "/computer/general/id/text()")

# Concatenate the first 4 chars of the building abd the computer's ID number

echo "$computerName"




New Contributor III
New Contributor III

Make sure that API user only has permission to read on computers. 

An alternate method is to dump your buildings and ID numbers with an advanced search, create the machine names in Excel/Numbers, export ID and machine name to a .csv, upload those to Jamf Pro using MUT, and create a policy to enforce the machine name. 

Thank you so much for your help @oliver  and @mickgrant !

One more thing, we've got a few buildings that we want use 3 or 4 letter acronyms for. I know I will need to use some sort of if/then statement to achieve this, but some guidance would be helpful and much appreciated. I'm looking for something like this I assume:


if building="Technology Asset Center"
then building="TAC"
else building="original string queried from API"


I've got about 10 campuses I need these if/then acronym statements set up for. Otherwise, it's basically working as hoped. 

We have a brand new deployment of 1600 macbooks coming soon. Our workflow (kind of Janky) is to put the devices into a pre-stage that has the location and position information baked in. I want my script to run after the enrollment with JAMF and leverage the API for this information on the back end. 

New Contributor III
New Contributor III

building="Technology Asset Center"

if [[ ${building} == "Technology Asset Center" ]]; then
elif [[ ${building} == "Main Campus" ]]; then
    echo "Warning -- No building matched"

echo "${building}"

# Prints "TAC"

Contributor II

At the end once I get the string, I'm sure I'll us scutil to rename the computer and disconfigad to bind to AD, but I need help getting this first part done. 

 instead of using scutil you should consider using the built-in jamf command instead jamf setComputerName
it does everything that is available within scutil, but it's a single line instead.

have a look at the documentation here - https://docs.jamf.com/10.26.0/jamf-pro/administrator-guide/Renaming_a_Computer.html