Question regarding changing computer name

binglenozumi
New Contributor II

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

 

2 ACCEPTED SOLUTIONS

ljcacioppo
Contributor III

Is there a reason you are doing the API call twice? For site name, you could just use the $response and pipe that to xpath

As for barcodeTag, I would personally use xpath for that as well, something like:
barcodeTag=$( echo $response | xmllint --xpath '/computer/general/barcode_1/text()' - )

Also, your serial command seemed to work on my computer, but this is what I've always used:

serialNumber=$(system_profiler SPHardwareDataType | grep Serial | awk '{ print $4 }')

 

View solution in original post

you could just do 

siteName=$( echo $response | xmllint --xpath 'computer/general/site/name/text()' - )

Since the $response variable is to the computer record entirely, it has both the site name and barcode info in that one call. Then you can just echo out the parts you need using the same response so you only need 1 API call per machine instead of 2

View solution in original post

5 REPLIES 5

ljcacioppo
Contributor III

Is there a reason you are doing the API call twice? For site name, you could just use the $response and pipe that to xpath

As for barcodeTag, I would personally use xpath for that as well, something like:
barcodeTag=$( echo $response | xmllint --xpath '/computer/general/barcode_1/text()' - )

Also, your serial command seemed to work on my computer, but this is what I've always used:

serialNumber=$(system_profiler SPHardwareDataType | grep Serial | awk '{ print $4 }')

 

Any better example for the sitename part? I wasn’t aware I can call the site just once to grab all the data first?

you could just do 

siteName=$( echo $response | xmllint --xpath 'computer/general/site/name/text()' - )

Since the $response variable is to the computer record entirely, it has both the site name and barcode info in that one call. Then you can just echo out the parts you need using the same response so you only need 1 API call per machine instead of 2

junjishimazaki
Valued Contributor

Whenever I script I like to echo the results of a certain statement to make sure it's doing whatever the statement is supposed to do. If it fails to retrieve the bar code and site name what error do you get? IAlso, for testing purposes, if you tried the script with your Jamf creds do you encounter any issues?

Actually I tried echo the result it shows no error, that’s why I cannot figure out which part creates the issue since only some device are having issue, some are not