Mass populate Extension Attribute

m_donovan
Contributor III

Our JSS has an extension attribute called "Computer Role" that is populated manually using a drop down menu. Is there a way that I can populate this such as how I can use jamf recon -room "104" to populate the room field?

5 REPLIES 5

freddie_cox
Contributor III

You could use the API and some scripting to accomplish this.

Example: If you had a CSV with a column that included serial number, name or JSS ID of the computer and another column that included the "role" you could build a loop that submitted the Extension Attribute for the individual computer object.

One thing to note: Since you are using a drop down menu, you will need to match the drop down options exactly in the second column. If you don't then it will cause an error in the assignment.

#!/bin/bash

# This is your unix line ending formated csv file of serial Numbers
# First Column is labeled serial, Second Column is labeled dropdown_value
INPUT=/Users/demo/Desktop/Extension_Attribute_Example.csv

# Set the file seperator to read the CSV correctly
OLDIFS=$IFS
IFS=,

# JSS URL to PUT based on Serial Number. This could be "id" or "name"
JSSURL="https://jss.yourcompany.com:8443/JSSResource/computers/serialnumber/"

# XML File that you're writing for each computer to upload using the API to make changes
XMLFILE=/tmp/demo_api.xml

# Basic Output in Terminal Window
echo "Running"

# Creates the XML File from above. Currently It's Blank
touch $XMLFILE

# This is where the magic happens. This is a bash loop.
# In English. If the Input file doesn't exists, spit out an error
[ ! -f $INPUT ] && { echo "$INPUT file not found"; exit 99;}

# Read the two column and make them usable as a variable
while read serial drop_downvalue
# Do what I tell you to do
do
    # Basic output in the shell window showing your progress
    # followed by the creation of the XML file using the variable and storing it in the XMLFILE location from above.
    echo "$serial $dropdown_value"
    echo "<computer><extension_attributes><extension_attribute><id>17</id><name>Extension Attribute Name</name><type>String</type><value>$dropdown_value</value></extension_attribute></extension_attributes></computer>" > $XMLFILE
    # This is the call to the API using cURL. It builds a unique URL for each Serial Number and PUT's the XML File in the request
    curl -k -v -u CasperAPI:kcs123z $JSSURL$serial -T $XMLFILE -X PUT

done < $INPUT
# Putting things back where we found them.
IFS=$OLFIFS

Note: The ID number in the XML will be different depending on which extension attribute you use. You can get this ID by going to Computer Management in the JSS, click on Extension Attributes, then click on the EA you are wanting to change. It will be in the URL like this: (I've bolded the ID number in this example)
https://jss.yourcompany.com:8443/computerExtensionAttributes.html?id=34&o=r

Obviously this isn't tested. If you have a test environment, i would recommend honing your script there. Also, start with a small list of 3-5 computers in your CSV file so you can get a feel for what is happening before tacking the full list.

m_donovan
Contributor III

freddie,

Thank you very much for your help. I will give this a try in our testing environment and see how it goes.

cdenesha
Valued Contributor II

Thank you for the sample code - time for me to figure this out too.

chris

JayDuff
Contributor II

For posterity: There is now an App in the Mac App Store, called The MUT. It will do exactly what you're looking for!

Geelcey
New Contributor III
New Contributor III

Hey, Sorry to open up something old but I have been playing around with the API stuff and I am aware of the MUT but wanted to do it via script.

I have tried the script above but Im getting error 400, surely this is relating to the XML input being malformed.

It will echo the serial and dropdown value but it doesn't like to PUT into the extension attribute. Has there been changes to the API in the 10.4.1 JAMF pro? I feel like when I was writing POST commands and taking notes I had to change some of the JSSResource variables.