i need some help with an API script

Contributor II

i have a computer extension attribute we use to set a computer's Role. its a drop down list. im trying to get a script to update that EA value in bulk off a .csv file of serial numbers.
im hitting a wall. i get no errors and it looks like it all runs fine, but it just wont update the EA value. i have the script running is a self service policy. i have pasted my script below. what do i have wrong?

also most of this is cut and paste from many other sources on the internet. mostly jamfnation


exec > /Library/pthsd/output.txt 2>&1


# This is your unix line ending formated csv file of serial Numbers
# First Column is labeled serial

INPUT=$(/usr/bin/osascript << EOF
tell application "System Events"
set INPUT to text returned of (display dialog "Enter the path to the .csv file with serial numbers" buttons {"Cancel", "Enter"} default button 2 default answer "")
end try
end tell

if [ "$INPUT" != "" ]; then
    echo "User input serial: $INPUT"
   echo "User canceled. Exiting"
   exit 0

echo "path to csv is $INPUT"

MacUserRole=$(/usr/bin/osascript << EOF
tell application "System Events"
set MacUserRole to text returned of (display dialog "Enter the desired value for Mac User Role" buttons {"Cancel", "Enter"} default button 2 default answer "")
end try
end tell

if [ "$MacUserRole" != "" ]; then
    echo "User input: $MacUserRole"
   echo "User canceled. Exiting"
   exit 0

echo "Desired Mac User Role is $MacUserRole"

# Set the file seperator to read the CSV correctly

# JSS URL to PUT based on Serial Number. This could be "id" or "name"

# XML File that you're writing for each computer to upload using the API to make changes

# 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;}

#lets create the xml file
    echo "<computer>
            <name>Mac User Role</name>
</computer>" > $XMLFILE

# Read the first column and make them usable as a variable
while read serial
# Do what I tell you to 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#.
    # 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 -sfku "$YourAPIUsername:$YourAPIPassword" "$JSSURL$serial" -H "Content-type: text/xml" -T $XMLFILE -X PUT

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

exit 0

Contributor II

nevermind. I was only trying to build this script because i didn't see the option in the MUT tool.    it turns out the feature i needed is in the MUT Classic tool.   so my initial problem is solved but i would still like to know why my script wasn't working.

New Contributor III

You need to add "/subset/extensionattributes" after the serial number in the API command. Give this a try. 

echo "$XMLFILE"  | curl -X PUT -sfku $YourAPIUsername:$YourAPIPassword -d @- "$JSSURL$serial/subset/extensionattributes" -H "Content-Type: application/xml"