Yes, you would need a script run that pulls those values out of the computer record in Jamf Pro using the API. IOW, the script will get the Mac's UUID or Serial Number and use that to look up it's own record, then grab the values you want from it, and finally, use those to set up the proper name, and name the computer.
I don't have enough time to write up an example script right this moment, but if you search for examples of pulling fields from an API computer record, you should find something you can adapt for this purpose.
If you have any trouble or can't locate something that works, post back and I'm sure someone will post an example script.
Thanks @mm2270 I appreciate the reply. I will see what I can dig up.
@mconners @mm2270 This script grabs a device's asset tag from the API and names the device accordingly. Just a little bit of tweaking needed to accomplish your goal.
Replace the API username and credentials with your account and this should work:
#!/bin/sh
serial=$(ioreg -c IOPlatformExpertDevice -d 2 | awk -F" '/IOPlatformSerialNumber/{print $(NF-1)}')
response=$(curl -k -H "accept: application/xml" -u apitest:api123 https://yourcompany.jamfcloud.com:443/JSSResource/computers/serialnumber/${serial}/subset/general)
assetTag=$(echo $response | /usr/bin/awk -F'<asset_tag>|</asset_tag>' '{print $2}');
barcode1=$(echo $response | /usr/bin/awk -F'< barcode_1 >|</barcode_1 >' '{print $2}');
barcode2=$(echo $response | /usr/bin/awk -F'< barcode_2 >|</barcode_2 >' '{print $2}');
computerName='MYCOMPANY-'$assetTag'-MAC-'$serial
# Setting computername
echo "Setting computer name..."
scutil --set ComputerName "$computerName"
scutil --set HostName "$computerName"
scutil --set LocalHostName "$computerName"
exit 0
Hi!
I am trying to use this script but want to name the PC based on the value in Barcode1, I thought I could change it like so
#!/bin/sh
serial=$(ioreg -c IOPlatformExpertDevice -d 2 | awk -F" '/IOPlatformSerialNumber/{print $(NF-1)}')
response=$(curl -k -H "accept: application/xml" -u apitest:api123 https://yourcompany.jamfcloud.com:443/JSSResource/computers/serialnumber/${serial}/subset/general)
assetTag=$(echo $response | /usr/bin/awk -F'<asset_tag>|</asset_tag>' '{print $2}');
barcode1=$(echo $response | /usr/bin/awk -F'< barcode_1 >|</barcode_1 >' '{print $2}');
barcode2=$(echo $response | /usr/bin/awk -F'< barcode_2 >|</barcode_2 >' '{print $2}');
computerName=$barcode1
# Setting computername
echo "Setting computer name..."
scutil --set ComputerName "$barcode1"
scutil --set HostName "$barcode1"
scutil --set LocalHostName "$barcode1"
exit 0
But if I put an echo at the end of the script
echo $barcode1
It shows as blank, despite the machine having a value in Barcode 1 in JAMF.
As another test, I put POTATO in the asset field for the computer in JAMF, and at the end of the script put echo $assetTag and it echoed POTATO..
Why is the above not working for naming the PC based on the value of Barcode1?
I found the issue...
there are spaces around the < barcode1 >
barcode1=$(echo $response | /usr/bin/awk -F'< barcode_1 >|</barcode_1 >' '{print $2}');
barcode2=$(echo $response | /usr/bin/awk -F'< barcode_2 >|</barcode_2 >' '{print $2}');
Why can't Jamf just look it up from Inventory Preload? Talk about a hassle.
I tried this script but it seems to not find the asset tag even though its tied to the machine. How can I fix?