Posted on 04-09-2024 02:53 PM
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?
Solved! Go to Solution.
Posted on 04-10-2024 08:04 AM
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"
Posted on 04-09-2024 04:15 PM
In the Jamf Pro API it mentions there is a "Return all sections of a computer" method which you can use via the following API endpoint. Documentation for the specific API endpoint is here:
https://developer.jamf.com/jamf-pro/reference/get_v1-computers-inventory-detail-id
The API endpoint does a lookup using the Mac computer ID instead of serial number however...
Posted on 04-09-2024 04:18 PM
Alternatively you could get inventory data for all Macs and then search for the specific Mac serial number, and then get the management ID:
https://developer.jamf.com/jamf-pro/reference/get_v1-computers-inventory
Posted on 04-10-2024 05:30 AM
If you look at what running "jamf recon" sends to stdout the last line contains the Jamf Pro ID for the computer.
Posted on 04-10-2024 08:04 AM
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"
Posted on 04-10-2024 02:59 PM
I got my script finished today. The goal was to have a script that could assign Macs to a site. Since I got some ideas from this thread, I posted my entire script there.
https://community.jamf.com/t5/jamf-pro/api-assign-computers-from-group-x-to-site-y/m-p/187368
Posted on 08-30-2024 01:36 AM
With no API used
sudo jamf recon | grep computer_id | cut -f2 -d '>' | cut -f1 -d'<'