Populate Computer Name based on Asset tag field?

mconners
Valued Contributor

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!

9 REPLIES 9

mm2270
Legendary Contributor III

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.

mconners
Valued Contributor

Thanks @mm2270 I appreciate the reply. I will see what I can dig up.

syncron
New Contributor II
New Contributor II

@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

aaronpolley
Contributor

mgoodall
New Contributor II

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?

mgoodall
New Contributor II

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}');

LswSysAdminJamf
New Contributor

Why can't Jamf just look it up from Inventory Preload? Talk about a hassle.

user-kDXbOofSkL
New Contributor

I tried this script but it seems to not find the asset tag even though its tied to the machine. How can I fix?

Lasse
New Contributor III

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