Posted on 04-30-2018 11:43 AM
Hello Folks,
We are looking at a slight change to our workflow and I have question I don't know if it can be done. If it can be done, how would I manage to do it.
Essentially, we have the ability to add to the Asset Tag field data. Through testing, it appears Asset tag along with Bar Code fields don't get wiped out once a computer is wiped and a new OS is loaded. We are experimenting with DEP and the best way to handle things. Once a computer is run through Pre stage enrollment, they are added with a generic place holder such as iMac or MacBook Pro. The bar code field remains in tact though.
The question is, is there a way to have the computer name auto populate with the information from the Asset tag field or the Bar Code field?
I suspect this would be a simple script. Anyone else have something like this out there? I don't want to reinvent the wheel here.
Thank you!
Posted on 04-30-2018 12:35 PM
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.
Posted on 04-30-2018 01:48 PM
Thanks @mm2270 I appreciate the reply. I will see what I can dig up.
Posted on 04-30-2018 08:34 PM
@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
Posted on 02-13-2020 05:35 AM
FWIW, use this to check if its named accordingly:
https://github.com/aarondavidpolley/ScriptRepo/blob/master/JamfPro-Casper-EA-ComputerName_AssetID.sh
Posted on 03-11-2021 08:45 AM
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?
Posted on 03-11-2021 09:09 AM
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}');
Posted on 04-23-2021 12:46 AM
Why can't Jamf just look it up from Inventory Preload? Talk about a hassle.
Posted on 06-29-2021 10:38 AM
I tried this script but it seems to not find the asset tag even though its tied to the machine. How can I fix?
01-13-2022 11:57 PM - edited 01-14-2022 01:54 AM
Edit: Yes, there has been and update to assetTag in Big Sur, continuing on thread below.
https://community.jamf.com/t5/jamf-pro/renaming-computers-using-asset-tag-data-api/td-p/30798