I modified an existing script (credit to Richard Purves) that runs successfully to change the computer name to the barcode using the Pro API on Monterey machines. My same script does not seem to work on Big Sur machines. I use the API to pull the barcode and then use that barcode to rename the computer. I initially thought it was because I was using the #!/bin/zsh shell, but I have switched it back to #!/bin/bash. Same error.
The error I get from the logs is as follows:
"Script result: execution error: Error: SyntaxError: JSON Parse error: Unterminated string (-2700)"
Clearly has something to do with the JSON Parse, but this is my first Pro API script so I have very little experience here.
Copy of my script:
#!/bin/bash
# POC script for Jamf Pro API
# Variables first
# API user accounts here. One for reading, one for writing back. Security.
# Generate API base64 credentials by using:
apib64="xxxxxxxxxxxxxxxxxxxxxxx"
PREFIX="UH"
SUFFIX="UOC"
# Current JSS address
jssurl=$( /usr/bin/defaults read /Library/Preferences/com.jamfsoftware.jamf.plist jss_url )
# Hardware UDID of the Mac you're running this on
udid=$( /usr/sbin/ioreg -rd1 -c IOPlatformExpertDevice | awk '/IOPlatformUUID/ { split($0, line, "\\""); printf("%s\\n", line[4]); }' )
# Use our base64 creds to generate a temporary API access token in JSON form
# Use tr to strip out line feeds or the JXA will not like the input
# Retrieve the read token from the JSON response
jsonresponse=$( /usr/bin/curl -s "${jssurl}api/v1/auth/token" -H "authorization: Basic ${apib64}" -X POST | tr -d "\\n" )
token=$( /usr/bin/osascript -l 'JavaScript' -e "JSON.parse(\\`$jsonresponse\\`).token" )
# Use the read token to find the ID number of the current Mac
computerrecord=$( /usr/bin/curl -s "${jssurl}api/v1/computers-inventory?section=GENERAL&filter=udid%3D%3D%22${udid}%22" -H "authorization: Bearer ${token}" )
BARCODE=$( /usr/bin/osascript -l 'JavaScript' -e "JSON.parse(\\`$computerrecord\\`).results[0].general.barcode1" )
echo "Barcode: $BARCODE"
sudo jamf setComputerName -name $PREFIX"$BARCODE"$SUFFIX
sudo jamf recon
# Ok we're done now.
# Invalidate the token
/usr/bin/curl -s -k "${jssurl}api/v1/auth/invalidate-token" -H "authorization: Bearer ${token}" -X POST
# All done
exit 0