Fill User Extension Attribute from Bash Script

jsmillie
New Contributor III

Warning up front my API wisdom is very very basic. I've had some luck here and there with other things, but first time trying to change/fill a user extension attribute. I have an extension attribute called "UserID_Number" which has ID number 1. I know this can be done as I've done it manually with "The MuTT" but unfortunately something I can automate is now required.

I figure I've got something wrong down below but after a few hours of smacking the desk I'm still lost. Any direction to the way that works would be greatly appreciated. Thanks.

#!/bin/sh
#Variables
file="/path/to/file.csv"                  #Path to the CSV
server=JSSSERVER                        #Server name
username=CAPABLEUSERNAME                            #JSS username with API privileges
password=CAPABLEUSERSPASSWORD                           #Password for the JSS account

user=no-one@gatewayk12.org
UserID=187  
JSS_API_INFO_DIR="/tmp/jss_api_tmp" # Directory where working files for each JSS ID will be stored
JSS_XML_INPUT="/tmp/JSS_XML_INPUT.xml" # XML Output to be uploaed to the JSS Computer Groups API

## Functions
#################################################################################################### 
function Update_UserID_Number () {
    curl -k -v -u $username:$password https://$server:8443/JSSResource/users/id/$JAMFID -T "$JSS_XML_INPUT" -X PUT
    echo "$?"
    echo "Done"
}

#Get Users JAMF ID 
JAMFID=$(curl -k -u $username:$password -H "Content-Type: text/xml" https://$server:8443/JSSResource/users/name/$user -X GET | xmllint --format - |grep id | head -n 1 |  sed -E 's/.*id.(.*)..id.*/1/')


#Top of the XML
echo '<?xml version="1.0" encoding="UTF-8" standalone="no"?>' > "$JSS_XML_INPUT"
echo "<extension_attributes>" >> "$JSS_XML_INPUT"
echo "<extension_attribute>" >> "$JSS_XML_INPUT"

echo "<id>1</id>" >> "$JSS_XML_INPUT"
#echo "<name>UserID_Number</name>" >> "$JSS_XML_INPUT"
#echo "<type>Number</type>" >> "$JSS_XML_INPUT"
echo "<value>$UserID</value>" >> "$JSS_XML_INPUT"
#done

#bottom of XML
echo "</extension_attribute>" >> "$JSS_XML_INPUT"
echo "</extension_attributes>" >> "$JSS_XML_INPUT"

#Make everything one continuous line
perl -pi -e 'tr/[�12�15]//d' "$JSS_XML_INPUT"


Update_UserID_Number

exit 0
1 ACCEPTED SOLUTION

dan-snelson
Valued Contributor II
2 REPLIES 2

dan-snelson
Valued Contributor II

jsmillie
New Contributor III

Indeed it was. Thank you!

For reference I was missing an XML header for "user".

#!/bin/sh
#Variables
file="/path/to/file.csv"                  #Path to the CSV
server=JSSSERVER                        #Server name
username=CAPABLEUSERNAME                            #JSS username with API privileges
password=CAPABLEUSERSPASSWORD                           #Password for the JSS account

user=no-one@gatewayk12.org
UserID=187  
JSS_API_INFO_DIR="/tmp/jss_api_tmp" # Directory where working files for each JSS ID will be stored
JSS_XML_INPUT="/tmp/JSS_XML_INPUT.xml" # XML Output to be uploaed to the JSS Computer Groups API

## Functions
#################################################################################################### 
function Update_UserID_Number () {
    curl -k -v -u $username:$password https://$server:8443/JSSResource/users/id/$JAMFID -T "$JSS_XML_INPUT" -X PUT
    echo "$?"
    echo "Done"
}

#Get Users JAMF ID 
JAMFID=$(curl -k -u $username:$password -H "Content-Type: text/xml" https://$server:8443/JSSResource/users/name/$user -X GET | xmllint --format - |grep id | head -n 1 |  sed -E 's/.*id.(.*)..id.*/1/')


#Top of the XML
echo '<?xml version="1.0" encoding="UTF-8" standalone="no"?>' > "$JSS_XML_INPUT"
echo "<user>" >> "$JSS_XML_INPUT"
echo "<extension_attributes>" >> "$JSS_XML_INPUT"
echo "<extension_attribute>" >> "$JSS_XML_INPUT"

echo "<name>UserID_Number</name>" >> "$JSS_XML_INPUT"
echo "<value>$UserID</value>" >> "$JSS_XML_INPUT"
#done

#bottom of XML
echo "</extension_attribute>" >> "$JSS_XML_INPUT"
echo "</extension_attributes>" >> "$JSS_XML_INPUT"
echo "</user>" >> "$JSS_XML_INPUT"

#bottom of XML
echo "</extension_attribute>" >> "$JSS_XML_INPUT"
echo "</extension_attributes>" >> "$JSS_XML_INPUT"

#Make everything one continuous line
perl -pi -e 'tr/[�12�15]//d' "$JSS_XML_INPUT"


Update_UserID_Number

exit 0