Posted on 11-17-2022 06:49 AM
Using the Jamf Pro API (not the classic), I'm trying to find out the best way to search for a specific device when you do not know the JSS ID.
So far the closest I can find is searching by mac, or mobile device, and then parsing through up to 2000 returned items. We have about 10K macs and 18K mobile devices so that isn't a very quick process. I was hoping to do it faster than that. The class API could search by serial number. Is that possible in the Jamf Pro API?
11-17-2022 07:06 AM - edited 11-17-2022 07:06 AM
This is how I was taught to do it in Jamf 300. In the example below, I'm targeting the computer using the serial number and uploading the install log to jamf pro. FYI this example does not use bearer tokens.
#!/bin/bash
username=$4
password=$5
jamfProURL=$6
#set the serial number of my mac to $macSerial
macSerial=$(system_profiler SPHardwareDataType | awk '/Serial Number/{print $4}')
# get my jamf pro computer id based on serial Number
myComputerID=$(curl -s -u $4:$5 -H "Accept: text/xml" $6/JSSResource/computers/serialnumber/"$macSerial" | xmllint --xpath '/computer/general/id/text()' -)
echo "$myComputerID"
# POST Upload the install.log to computer based on myComperID
curl -u $4:$5 $6/JSSResource/fileuploads/computers/id/"$myComputerID" -F name=@/private/var/log/install.log -X POST
11-17-2022 07:10 AM - edited 11-17-2022 07:11 AM
Here's a more advanced example that uses bearer tokens (recommended for security purposes). Again I'm targeting the desired machine based on the serial number and telling it to force a software update.
#!/bin/bash
# Server connection information
URL="https://url.jamfcloud.com"
username="apiusername"
password="apipassword"
# Determine Serial Number
serialNumber=$(system_profiler SPHardwareDataType | awk '/Serial Number/{print $4}')
initializeSoftwareUpdate(){
# create base64-encoded credentials
encodedCredentials=$( printf "${username}:${password}" | /usr/bin/iconv -t ISO-8859-1 | /usr/bin/base64 -i - )
# Generate new auth token
authToken=$( curl -X POST "${URL}/api/v1/auth/token" -H "accept: application/json" -H "Authorization: Basic ${encodedCredentials}" )
# parse authToken for token, omit expiration
token=$(/usr/bin/awk -F \" 'NR==2{print $4}' <<< "$authToken" | /usr/bin/xargs)
echo ${token}
# Determine Jamf Pro device id
deviceID=$(curl -s -H "Accept: text/xml" -H "Authorization: Bearer ${token}" ${URL}/JSSResource/computers/serialnumber/"$serialNumber" | xmllint --xpath '/computer/general/id/text()' -)
echo ${deviceID}
# Execute software update
curl -X POST "${URL}/api/v1/macos-managed-software-updates/send-updates" -H "accept: application/json" -H "Authorization: Bearer ${token}" -H "Content-Type: application/json" -d "{\"deviceIds\":[\"${deviceID}\"],\"maxDeferrals\":0,\"version\":\"12.3.1\",\"skipVersionVerification\":true,\"applyMajorUpdate\":true,\"updateAction\":\"DOWNLOAD_AND_INSTALL\",\"forceRestart\":true}"
# Invalidate existing token and generate new token
curl -X POST "${URL}/api/v1/auth/keep-alive" -H "accept: application/json" -H "Authorization: Bearer ${token}"
}
initializeSoftwareUpdate
11-17-2022 02:13 PM - edited 11-17-2022 02:15 PM
If I'm understanding the question correctly, there isn't a way to do it easily -- unfortunately the case with some Pro API calls. That said, I wrote some python code only using the new API that loads all computers (no 2000-computer limit) and then searches and prints the computer that matches the given serial number. On my instance, admittedly smaller than yours, it only takes a few seconds -- not as good as the Classic, but not too bad:
https://github.com/MatthewPrins/Jamf/blob/main/Computer_Serial_New_API.py
Posted on 11-17-2022 02:29 PM
I suppose I would ask if you're only searching for a single device, why not use the JPS GUI?
Posted on 11-17-2022 06:40 PM
The reason for a specific device is mainly so some of our field techs, who don't have access to Jamf directly, can perform a couple basic functions such as Renaming an Apple TV after they configure/install one. I have a working model of it with the classic now but I'm trying to use more of the newer API since that's clearly going to be the one that gets development.
Posted on 11-18-2022 10:25 AM
You can go hybrid and pull the computer ID using the classic, then use the computer inventory detail patch command on the new API, so you're at least half future-proofed. I have a few shell scripts that use both APIs together for various reasons.
Posted on 11-19-2022 07:01 AM
I have found a way that works pretty well using only the new API. I was looking through the documentation and noticed you can sort by the different properties. Including the model. For our 18K mobile devices, only about 1800 are actually Apple TVs. So I can sort by the model and at least they will always return on the first page. Or, as we gain more, on the second.
&sort=model%3Aasc
Posted on 04-01-2024 04:49 AM
Since jamf deprecated teh classic API .. how is it possible to get the serial number using Jamf Pro API?