Skip to main content
Solved

Fill User Extension Attribute from Bash Script


Forum|alt.badge.img+5

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

Best answer by dan-snelson

This post may prove helpful: Your Internal Beta Test Program: Opt-in / Opt-out via Self Service

View original
Did this topic help you find an answer to your question?

2 replies

dan-snelson
Forum|alt.badge.img+28
  • Honored Contributor
  • 627 replies
  • Answer
  • May 18, 2017

Forum|alt.badge.img+5
  • Author
  • New Contributor
  • 7 replies
  • May 18, 2017

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/[1215]//d' "$JSS_XML_INPUT"


Update_UserID_Number

exit 0

Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings