API Newbie here asking for help, please :-)

HariSeldon
New Contributor III

Hi All,

 

I'm new to the API and newish with scripting, so please bear with me.

My end goal is: have a list of all the Mobile Device Applications and what they are scoped to.

 

I've tried this for loop:

#!/bin/bash

IDs=$(curl -sku myusername:mypassword -H "content-type: text/xml" https://myinstance:8443/JSSResource/mobiledeviceapplications | xmllint --format - | awk -F '[<>]' '/<id>/{print $3}')

for idNumber in $IDs
do

	curl -sku myusername:mypassword -X GET -H "Accept: application/xml" https://myinstance:8443/JSSResource/mobiledeviceapplications | xmllint --xpath "/mobile_device_application/scope/text()" -

done

 

Though it tells me that the "Xpath is empty".
Is there a way for me to reiterate through the Application IDs I find using curl, and show me the scope for each and all of them?

 

Thank you

1 ACCEPTED SOLUTION

HariSeldon
New Contributor III

Thank you @mm2270. I wasn't aware of that, although I can confirm that also previous version of xpath work on Big Sur.

 

At the end I figured out how to have the list of apps and their scope. The result is not perfect but works for me, and I'm learning a lot from this.

Here is the code that I made finally:

 

 

 

#!/bin/bash

# 1) Author: Lorenzo Fittarelli
# 2) Created: 16/12/2021
# 3) Modified: 16/12/2021
# 4) Description: Outputs the list of Mobile device Applications and their scope
# 5) Usage: ./mobDevAppsScope.sh

user=<yourUserName>
pass=<yourPassword>


while read column1; do
	
# Each of the variables below has a command that extracts the scope for the apps. The scope includes multiple tags, therefore I had to create a command for each of them. 

	appScope1=$(curl -sku $user:$pass -X GET -H "Accept: application/xml" "https://<yourJamfInstance>/JSSResource/mobiledeviceapplications/name/$column1" | xmllint --xpath "/mobile_device_application/scope/all_mobile_devices/text()" - | awk 'BEGIN { OFS = ";"; ORS = "\n\n" }{ print $0 }')
	appScope2=$(curl -sku $user:$pass -X GET -H "Accept: application/xml" "https://<yourJamfInstance>/JSSResource/mobiledeviceapplications/name/$column1" | xmllint --xpath "/mobile_device_application/scope/all_jss_users/text()" - | awk 'BEGIN { OFS = ";"; ORS = "\n\n" }{ print $0 }')
	appScope3=$(curl -sku $user:$pass -X GET -H "Accept: application/xml" "https://<yourJamfInstance>/JSSResource/mobiledeviceapplications/name/$column1" | xmllint --xpath "/mobile_device_application/scope/mobile_devices/mobile_device/name/text()" - | awk 'BEGIN { OFS = ";"; ORS = "\n\n" }{ print $0 }')
	appScope4=$(curl -sku $user:$pass -X GET -H "Accept: application/xml" "https://<yourJamfInstance>/JSSResource/mobiledeviceapplications/name/$column1" | xmllint --xpath "/mobile_device_application/scope/buildings/building/name/text()" - | awk 'BEGIN { OFS = ";"; ORS = "\n\n" }{ print $0 }')
	appScope5=$(curl -sku $user:$pass -X GET -H "Accept: application/xml" "https://<yourJamfInstance>/JSSResource/mobiledeviceapplications/name/$column1" | xmllint --xpath "/mobile_device_application/scope/departments/department/name/text()" - | awk 'BEGIN { OFS = ";"; ORS = "\n\n" }{ print $0 }')
	appScope6=$(curl -sku $user:$pass -X GET -H "Accept: application/xml" "https://<yourJamfInstance>/JSSResource/mobiledeviceapplications/name/$column1" | xmllint --xpath "/mobile_device_application/scope/mobile_device_groups/mobile_device_group/name/text()" - | awk 'BEGIN { OFS = ";"; ORS = "\n\n" }{ print $0 }')

# Output to a CSV file the name of the Mobile Device App (column1 variable) and the scopes. Each scope is divided here by a column so that in the CSV file it appears in different columns.
	
	echo "$column1" , "$appScope1" , "$appScope2" , "$appScope3" , "$appScope4" , "$appScope5" , "$appScope6" 
	
	
	# The while loop takes as input the CSV file that provides the Mobile Device App Names and redirects the output to a new CSV file.
	# any error is output into an error log txt file
	
done < <pathToInputCSV> > <pathToOutputCSV> 2>> <pathToErrorLog>

 

 

 

I hope it helps anyone who's looking for the same result. And of course, I'm happy to receive constructive critics to my code  🙂

View solution in original post

2 REPLIES 2

mm2270
Legendary Contributor III

If this is being run against a Mac running macOS Big Sur, you may want to take a look at this article, which explains how xpath now works in that OS.

https://scriptingosx.com/2020/10/dealing-with-xpath-changes-in-big-sur/

HariSeldon
New Contributor III

Thank you @mm2270. I wasn't aware of that, although I can confirm that also previous version of xpath work on Big Sur.

 

At the end I figured out how to have the list of apps and their scope. The result is not perfect but works for me, and I'm learning a lot from this.

Here is the code that I made finally:

 

 

 

#!/bin/bash

# 1) Author: Lorenzo Fittarelli
# 2) Created: 16/12/2021
# 3) Modified: 16/12/2021
# 4) Description: Outputs the list of Mobile device Applications and their scope
# 5) Usage: ./mobDevAppsScope.sh

user=<yourUserName>
pass=<yourPassword>


while read column1; do
	
# Each of the variables below has a command that extracts the scope for the apps. The scope includes multiple tags, therefore I had to create a command for each of them. 

	appScope1=$(curl -sku $user:$pass -X GET -H "Accept: application/xml" "https://<yourJamfInstance>/JSSResource/mobiledeviceapplications/name/$column1" | xmllint --xpath "/mobile_device_application/scope/all_mobile_devices/text()" - | awk 'BEGIN { OFS = ";"; ORS = "\n\n" }{ print $0 }')
	appScope2=$(curl -sku $user:$pass -X GET -H "Accept: application/xml" "https://<yourJamfInstance>/JSSResource/mobiledeviceapplications/name/$column1" | xmllint --xpath "/mobile_device_application/scope/all_jss_users/text()" - | awk 'BEGIN { OFS = ";"; ORS = "\n\n" }{ print $0 }')
	appScope3=$(curl -sku $user:$pass -X GET -H "Accept: application/xml" "https://<yourJamfInstance>/JSSResource/mobiledeviceapplications/name/$column1" | xmllint --xpath "/mobile_device_application/scope/mobile_devices/mobile_device/name/text()" - | awk 'BEGIN { OFS = ";"; ORS = "\n\n" }{ print $0 }')
	appScope4=$(curl -sku $user:$pass -X GET -H "Accept: application/xml" "https://<yourJamfInstance>/JSSResource/mobiledeviceapplications/name/$column1" | xmllint --xpath "/mobile_device_application/scope/buildings/building/name/text()" - | awk 'BEGIN { OFS = ";"; ORS = "\n\n" }{ print $0 }')
	appScope5=$(curl -sku $user:$pass -X GET -H "Accept: application/xml" "https://<yourJamfInstance>/JSSResource/mobiledeviceapplications/name/$column1" | xmllint --xpath "/mobile_device_application/scope/departments/department/name/text()" - | awk 'BEGIN { OFS = ";"; ORS = "\n\n" }{ print $0 }')
	appScope6=$(curl -sku $user:$pass -X GET -H "Accept: application/xml" "https://<yourJamfInstance>/JSSResource/mobiledeviceapplications/name/$column1" | xmllint --xpath "/mobile_device_application/scope/mobile_device_groups/mobile_device_group/name/text()" - | awk 'BEGIN { OFS = ";"; ORS = "\n\n" }{ print $0 }')

# Output to a CSV file the name of the Mobile Device App (column1 variable) and the scopes. Each scope is divided here by a column so that in the CSV file it appears in different columns.
	
	echo "$column1" , "$appScope1" , "$appScope2" , "$appScope3" , "$appScope4" , "$appScope5" , "$appScope6" 
	
	
	# The while loop takes as input the CSV file that provides the Mobile Device App Names and redirects the output to a new CSV file.
	# any error is output into an error log txt file
	
done < <pathToInputCSV> > <pathToOutputCSV> 2>> <pathToErrorLog>

 

 

 

I hope it helps anyone who's looking for the same result. And of course, I'm happy to receive constructive critics to my code  🙂