I'm working creating a script that performs several functions. One of the functions requires me to obtain the computer's Jamf Pro ID. I'm trying to figure out how to do this using the new API. I thought if I have a variable for the computer's serial number I could use that to query for the Jamf Pro ID. Is this possible?
How do I obtain a computer's Jamf Pro computer ID using API?
Best answer by howie_isaacks
Thanks for the suggestions. On another Jamf Nation thread, I found this command:
deviceID=$(curl -s -H "Accept: text/xml" -H "Authorization: Bearer ${token}" ${jamfProURL}/JSSResource/computers/serialnumber/"$serialNumber" | xmllint --xpath '/computer/general/id/text()' -)
I tried it and it did return my computer ID. I am very new to the API. It's the only major feature of Jamf Pro I have not had a chance to dive into. Comparing the URL above to other API URLs I have used this one appears to be the "classic" API. I can't seem to do this with the newer API. Either I'm still too inexperienced with the API to figure it out or it's not possible in the new API? The ultimate goal is to assign computers to a specific site after enrollment. We use LDAP to fill in information into User and Location which would make the computer fall into a smart group that can be scoped to a policy to assign the computer to one of our sites. This is necessary because we have admins who can only see computers in the sites that they have access to. Using the API appears to be the only way I can automate this. We could assign computers to PreStages that will auto assign the computers to specific sites but this won't work for us since we don't always know where computers are going to be deployed and it would be a nightmare trying to manage that. Here's the script that I created from all that I have learned so far. The final result is that it provides the Jamf Pro ID for my computer.
#!/bin/zsh
serialNumber=$(system_profiler SPHardwareDataType | grep Serial | /usr/bin/awk '{ print $4 }')
# API login
jamfProURL="https://YourServer.jamfcloud.com"
username="apiUser"
password="SuperDuperSecretPassword"
# request auth token
authToken=$( /usr/bin/curl \\
--request POST \\
--silent \\
--url "$jamfProURL/api/v1/auth/token" \\
--user "$username:$password" )
# parse auth token
token=$( /usr/bin/plutil \\
-extract token raw - <<< "$authToken" )
# determine Jamf Pro device id
deviceID=$(curl -s -H "Accept: text/xml" -H "Authorization: Bearer ${token}" ${jamfProURL}/JSSResource/computers/serialnumber/"$serialNumber" | xmllint --xpath '/computer/general/id/text()' -)
echo "$deviceID"
Reply
Enter your E-mail address. We'll send you an e-mail with instructions to reset your password.