Help with my first script to pull information from API

coachdnadel
New Contributor III

I am trying to write a script that will pull the mobile device application id and scope from the API. I'd then like to add a smart group to the scope of certain apps based on their current scoped groups. I have a script that will pull all of my app ids and a script that will pull the scope of an individual app, but I don't know how to create a script that will be able to loop through all of my apps and output the app with its scope.

Pull id for all Apps

#!/bin/bash

jamfUser="api-read"
jamfPass="**********"
jamfURL="https://******.jamfcloud.com/JSSResource"

apiData=$( /usr/bin/curl --user "$jamfUser":"$jamfPass" 
--header "Accept: text/xml" 
--request GET 
$jamfURL/mobiledeviceapplications

echo $apiData | xmllint --format - | xpath "/mobile_device_applications/mobile_device_application" | grep -e "<id>" | awk -F "<id>|</id>" '{print $2}'

Pull scope for specific id

#!/bin/bash

jamfUser="api-read"
jamfPass="*********"
jamfURL="https://*********.jamfcloud.com/JSSResource"
appID="265"

apiData=$( /usr/bin/curl --user "$jamfUser":"$jamfPass" 
--header "Accept: text/xml" 
--request GET 
$jamfURL/mobiledeviceapplications/id/$appID/subset/Scope )

echo $apiData | xmllint --format - | xpath "/mobile_device_application/scope/mobile_device_groups/mobile_device_group" | grep -e "<name>" | awk -F "<name>|</name>" '{print $2}'

I've added the appID variable because I assume there is a way to write a loop that will loop through all my IDs using some kind of appID + 1 variation.

1 REPLY 1

mm2270
Legendary Contributor II

Something like this maybe? I don't have mobile device applications in our JSS, so I don't have a good reference to go by.

#!/bin/bash

jamfUser="api-read"
jamfPass="**********"
jamfURL="https://******.jamfcloud.com/JSSResource"

IDS=$(/usr/bin/curl -H "Accept: text/xml" --user "$jamfUser":"$jamfPass" $jamfURL/mobiledeviceapplications | xmllint --format - | awk -F'>|<' '/<id>/{print $3}')

while read ID; do
    echo "$(/usr/bin/curl -H "Accept: text/xml" --user "$jamfUser":"$jamfPass" $jamfURL/mobiledeviceapplications/${ID} | xmllint --format - | awk -F'>|<' '/<name>/{print $3}')"
done < <(printf '%s
' "$IDS")

I have a feeling there's a better way to extract just the ids or names with pure xpath, but it wasn't working well for me with a computer smart group as a test. xpath seems to get tripped up on some computer names with non-standard characters in them, like apostrophes.