i need some help with an API script

jchurch
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

 

#!/bin/bash
exec > /Library/pthsd/output.txt 2>&1

YourAPIUsername="$4"
YourAPIPassword="$5"

# 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"
try
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
EOF)

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

echo "path to csv is $INPUT"

MacUserRole=$(/usr/bin/osascript << EOF
tell application "System Events"
try
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
EOF)

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

echo "Desired Mac User Role is $MacUserRole"





# 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-01.pthsd.k12.nj.us:8443/JSSResource/computers/serialnumber/"

# XML File that you're writing for each computer to upload using the API to make changes
XMLFILE=/tmp/temp_api_values.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;}


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


# Read the first column and make them usable as a variable
while read serial
# 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#.
    # 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.
IFS=$OLFIFS

exit 0
2 REPLIES 2

jchurch
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.

skeenan07
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"