Pull asset tag information from JSS record for use in Bash scripts

rbingham917
New Contributor III

I am working on doing a fully one touch set up for my company and one of the road blocks that I am facing is how to set the machine name using the asset tag information pulled from that computers record in the JSS, and use it in a script to add either a "LM" if it is a macbook type product, or a "DM" if it is an iMac type device. EX- LM1234 or DM5678

I can script everything except for the asset tag information. Since we are using DEP, we can input the asset tag information before the machine gets to the client, but I would like to make sure that we have the machine name set according to that, so I don't have machines bound to AD with blahblahblah's Macbook Pro.

Thanks. Running JSS 9.91

8 REPLIES 8

mm2270
Legendary Contributor III

You're going to need to look at accessing it via the JSS API, since its stored in the JSS, not on the local machine, as I'm sure you've already figured out.

The general idea would be to do something like the following in a script.
1. Get the Mac's Serial Number or UUID/UDID or MAC Address
2. Use API "read" account credentials in a script (these can be passed to the script as script parameters) and do an API call against your JSS using one of the above items to locate the computer
3. Use some command line tools to extract the Asset tag string from the computer record the API call returns, and store that in a variable which can be used later to rename the machine

Here is an example of getting the Asset tag string

#!/bin/bash

## Enter the API Username, API Password and JSS URL here
apiuser="apiuser"
apipass="apiuser"
jssURL="https://my.jss.company.com:8443"

## Get the Mac's UUID string
UUID=$(ioreg -rd1 -c IOPlatformExpertDevice | awk -F'"' '/IOPlatformUUID/{print $4}')

## Pull the Asset Tag by accessing the computer records "general" subsection
Asset_Tag=$(curl -H "Accept: text/xml" -sfku "${apiuser}:${apipass}" "${jssURL}/JSSResource/computers/udid/${UUID}/subset/general" | xmllint --format - 2>/dev/null | awk -F'>|<' '/<asset_tag>/{print $3}')

echo "$Asset_Tag"

rbingham917
New Contributor III

Thanks a lot @mm2270 This worked like a charm.

I really need to get up to snuff in this scripting thing...

joethedsa
Contributor II

I'm looking to do sort of the same thing but in two stages. Is there a way to populate the Asset Tag in JSS using the API only extracting some of the current computer's hostname? For example, if a computer name is "iMac-123456". I would like to populate the Asset Tag to be "123456". Our naming follows the same format "model-123456" in our environment. Therefore, only grabbing whatever is after a "dash" in our name would be the Asset Tag.

From there, I would like to be able to rename a computer to be "model-AssetTag" this would help automate the naming of wiped computers that are already in JSS and have an Asset Tag. The model would follow the following hardware criteria:

iMac = imac
MacBook Pro = mbp
MacBook Air = mba
MacBook Pro Retina = mbpr
Mini = mini

There must be a way to automate the naming of a computer based on it's Asset Tag (assuming the field is populated in JSS).

Tribruin
Valued Contributor
Valued Contributor

@joethedsa , for the first part of your question, you can use the following command to pull the asset tag from the computer name:

assetTag=$(echo $(scutil --get LocalHostName) | awk -F"-" '{print $2}')

That will echo everything after the - to the variable assetTag.

As far as renaming based on model and asset tag. Once you have the Asset Tag from the API call, you can you the following command to get the model information:
ioreg -c IOPlatformExpertDevice -d 2 | awk -F" '/model/{print $(NF-1)}'

You will get an output similar to MacBookPro14,3. You could parse the output before the numbers to determine the model type (MBP, MBA, iMac, etc.).

mm2270
Legendary Contributor III

@joethedsa I'd use a case statement in the script to figure out which prefix to use depending on the model identifier information. The only issue I see with this is identifying the Macs with Retina displays as you mention. The model identifier strings make no mention of the type of display, so you aren't going to be able to use that to determine which are "Retina" vs which are not. If you're ok with just naming any MacBookPro to "mbp" as it's prefix, here is an example of how to do that. This is not a complete script. You'll need to incorporate this into some other script or add to it to make it more complete.

model_name=$(sysctl hw.model | awk '{print $NF}' | tr -cd '[[:alpha:]]')

case $model_name in
       MacBookPro*)
       prefix="mbp" ;;
       MacBookAir*)
       prefix="mba" ;;
       iMac*)
       prefix="imac" ;;
       Macmini*)
       prefix="mini" ;;
       *)
       prefix="mac" ;;
esac

computer_name="$prefix-$assettag"

scutil --set ComputerName "$computer_name"
scutil --set LocalHostName "$computer_name"
etc...

You'd need to use some other syntax as shown in examples above to get the asset tag string into the $assettag variable somewhere in the script.

joethedsa
Contributor II

Thank you @mm2270 and @RBlount! Between the two scripts you've shared, I should be able to build something with them.

If the Asset Tag doesn't live in JSS for a computer yet, is there an API connection that would "write" to JSS from the command line for the device record that you are running the command from? In other words, if I wanted to create a policy to run at check-in on a group of computers that would populate the Asset Tag by using some of the computer's host name, do you know if that is even possible or does the Asset Tag have to be entered in the web GUI JSS?

Tribruin
Valued Contributor
Valued Contributor

You don't need to use the API. sudo jamf recon -assetTag XXXXX will populate the asset tag field.

mm2270
Legendary Contributor III

Hi @joethedsa
Yes! It’s absolutely possible to do that, and the best news is you don’t need to do any fancy API scripting to do it. Take a look at the jamf recon command. There’s a flag called -assetTag I believe, that let’s you pass a string to it. So while collecting inventory it will populate the asset tag for the machine with whatever string you pass to it.

The only possibly tricky part of this is determining what the asset tag is for the computer. But I’m guessing you have some way to determine that in a script?