Working with the JSS API Pt II

This is a follow up to my previous post on the subject which contains some useful info on reading from the JSS.

Since I wrote that we have been hard at work expanding our use of the JSS api and have written some useful reusable script snippets to assist us.

As we now try, wherever possible, to run our scripts directly from a policy we pass the api user details as variables so would set this up in the script like so,

apiusername="$5" #script api user
apipassword="$6" #api user password

We set variables for any Extension Attributes that we intend to write to later,

EAOurEA1="our_ea1"
EAOurEA2="our_ea2"

These wil be used in our routines later.

Writing EA routine.

We use a standard routine to write our EA, this is called by other routines and gives a status code to show it’s succesful (or not)

jssUpdate() {

apiUsername="$1"
apiPass="$2"
jssAddress="$3"
jssDevice="$4" # this uses UDID as it is unique, serial or computer name can be wrong
EAName="$5" # display name of the EA
EAValue="$6" # what the value to set is

mdmWriteResult=$(curl --write-out \\n%{http_code} --output /dev/null -H "Content-Type: application/xml" -sku "$apiUsername":"$apiPass" "$jssAddress"JSSResource/computers/udid/"$jssDevice" -d "$EAName$EAValue" -X PUT )

mdmWriteResult=$(echo $mdmWriteResult | sed '/^$/d')
echo "EA write return code is $mdmWriteResult"

if [ "$mdmWriteResult" = "201" ]; then
echo "MDM EA Write ** $EAName ** ok"
else 
echo "Failed to Write MDM EA ** $EAName ** "
fi
}

This is written to give a positive indication if a 201 result, which indicates a sucessful write, is returned.

To call this routine we use other simple routines that gather all the required info and pass it through,

valueSetYes() {
# setting our EA to yes
echo "Setting EA to yes"
jssUpdate "$apiusername" "$apipassword" "$jssurl" "$udid" "$EAOurEA1" "yes"

}

This example routine illustrates the method perfectly. Here we pass the apiuser,  password, JSS url, the udid (for unique record selection), the variable for the EA, the value we set.

So we can use different one line routines to suit whatever we want, the value can itself be a variable of course if that suits.

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s