Hi everyone,
I have created a script to rename computer name using classic API to get the device's site name and barcode_1 tag, however, it seems like intermittently it will fail for the script, usually, most of the time I see fail is unable to retrieve barcode_1 tag, sometimes it will fail at retrieving the site name. Can anyone have a look at the script and provide me some suggestions to improve as keep fixing the name manually is something I don't really want to do... Thank you so much
#!/bin/sh
jssToken='token for access'
jssURL="https://your.jamfurl.com"
serial=$(ioreg -c IOPlatformExpertDevice -d 2 | awk -F\\" '/IOPlatformSerialNumber/{print $(NF-1)}')
response=$(curl -X GET -s -H 'authorization: Basic\\'${jssToken}'' "$jssURL/JSSResource/computers/serialnumber/${serial}/subset/general")
barcodeTag=$(echo $response | /usr/bin/awk -F'<barcode_1>|</barcode_1>' '{print $2}');
siteName=$(curl -X GET -s -H 'authorization: Basic\\'${jssToken}'' "$jssURL/JSSResource/computers/serialnumber/${serial}/subset/general" | xmllint --xpath 'computer/general/site/name/text()' -)
computerName='CompanyName - '$siteName' '$barcodeTag''
localName=$(scutil --get ComputerName)
if [[ $localName == $computerName ]]; then
echo "Name is correct, no changes required"
else
# Setting computer name
scutil --set ComputerName "$computerName"
scutil --set ComputerName "$computerName"
scutil --set ComputerName "$computerName"
fi