I am having issues updating a classic API script with the bearer token. Testing the script below I seem to have an issue somewhere as the variable will not propagate accordingly. I am getting "MBA-1" every time I run the script.
# Getting the computer's serial number to make the API call
serialnumber=$(system_profiler SPHardwareDataType | awk '/Serial/ {print $4}')
# Decrypting the string above
function DecryptString() {
echo "${1}" | /usr/bin/openssl enc -md md5 -aes256 -d -a -A -S "${2}" -k "${3}"
}
string=$(DecryptString $EncryptedString $Salt $Passphrase)
model=$(system_profiler SPHardwareDataType | grep "Model Identifier" | awk '{print $3}' | sed 's/[1-9].*$//')
case $model in
MacBookPro) short=MBS ;;
MacBook) short=MB ;;
MacBookAir) short=MBA ;;
iMac) short=IMC ;;
*) short=UNK ;;
esac
# A basic API Call that's getting information for the computer.
# computerxml=$(curl -s -H “Authorization: Bearer ${token}” -H ${jamfurl}/JSSResource/computers/serialnumber/${serialnumber} -X GET)
# Finding a specific component of the XML using xpath (in this case, the ID)
# id=$(echo $computerxml | xpath 'string(/computer/general/id)')
# Create an array of Computer Names in Jamf
var=$(curl -s -H “Authorization: Bearer ${token}” -H ${jamfurl}/JSSResource/computers -X GET | tidy -xml | grep '<name>' | sed -n 's|<name>\\(.*\\)</name>|\\1|p' | grep $short | cut -d'-' -f 2)
# Add computer names to array called "name"
name=($var)
# Adds the number of values in array to varialbe "namen"
namen=${#name[@]}
# Adds the highest number in array to variable "hinumber"
IFS=$'\\n'
hinumber=$(echo "${name[*]}" | sort -nr | head -n1)
# Adds the highest number +1 to a varialbe "NUM"
NUM=$(($hinumber+1))
computerName=${short}-${NUM}
serialnumber=$(system_profiler SPHardwareDataType | awk '/Serial/ {print $4}')
curl -s -H “Authorization: Bearer ${token}” -H "Content-Type: application/xml" ${jamfurl}/JSSResource/computers/serialnumber/${serialnumber} -d "<computer><general><name>${computerName}</name></general></computer>" -X PUT
scutil --set ComputerName ${computerName}
scutil --set HostName ${computerName}
scutil --set LocalHostName ${computerName}
echo "Computer name changed to $computerName"
jamf displayMessage -message "The computer name is ${computerName} - Please make a label reflecting this. "
The script result reads: No warnings or errors were found. Not sure if the issue is syntax related or with the bearer authentication.