API output

Jdrella17
New Contributor II

How do i get the output of a specific category to write on separate lines rather than in one line? For example, if I want to get the name of all the configuration profiles on the mobile devices it will display each device on the same line but i would like it on individual lines. Heres is what I have tried:

curl -H "Accept: application/xml" -su "username:password" https://your.jamf.pro.url:8443/JSSResource/mobiledeviceconfigurationprofiles" -H "accept: application/xml" | xmllint --xpath "/configuration_profiles/configuration_profile/name/text()" -

I'm pretty new working in shell and posting to the forum so I apologize if I didn't follow rules or post correctly. I appreciate any help that anyone can provide. Thank you.

6 REPLIES 6

nelsoni
Contributor III

I got it to work by first getting a list of all static groups they are in

JAMF_ALLCOMPUTERID=$(curl -H "Accept: application/xml" -sfku "${apiuser}:${apipass}" -X GET "${jamfProURL}/JSSResource/computermanagement/serialnumber/${SerialNumber}/subset/StaticGroups" | xpath -e '/computer_management/static_groups/group/id' 2>/dev/null)

Then I had to format it after, I am sure this is not best practice but it works.

echo $JAMF_ALLCOMPUTERID | sed -e 's/<id>//g' -e 's/</id>/'$' /g'

mm2270
Legendary Contributor III

Can you explain what the end goal is here? Where are you using or outputting the list of profiles to?

As an alternative, you could swap out xpath for xmllint --format - and throw this on the end of the curl command

| xmllint --format - | awk -F'>|<' '/<name>/{print $3}'

Meaning the full command would look like

curl -H "Accept: application/xml" -su "username:password" https://your.jamf.pro.url:8443/JSSResource/mobiledeviceconfigurationprofiles" -H "accept: application/xml" | xmllint --format - | awk -F'>|<' '/<name>/{print $3}'

That will get you each entry on its own line.

I would also love to know how to handle multiple line returns correctly when using xpath like shown, because I've run into the same issue with it and never found a good answer to the problem.

Tribruin
Valued Contributor
Valued Contributor

You can use the xmltproc tool to do this, but you have to create a "template" on how you want capture the output. I just checked and this template should work in your case:
create a text file using the text below and save it on your computer (e.g. ~/Desktop/configprofile.xslt)

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:template match="/">
<xsl:for-each select="configuration_profiles/configuration_profile">
    <xsl:value-of select="name"/>
    <xsl:text>&#xa;</xsl:text>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>

Then change your command to the following:

curl -H "Accept: application/xml" -su "username:password" https://your.jamf.pro.url:8443/JSSResource/mobiledeviceconfigurationprofiles" -H "accept: application/xml" | xsltproc ~/Desktop/configprofile.xslt -

Jdrella17
New Contributor II

@mm2270 Unfortunately that code didnt work for me. I just copied what you provided and got the error: "Unknown option --
-:1: parser error : Start tag expected, '<' not found
Usage : xmllint [options] XMLfiles ...
^
(23) Failed writing body" when i try running it in VS Code. Also, I am only trying to get information for reports when i need them, and having them output on screen, .txt or .csv on individual lines is just more aesthetically pleasing to me. If there is an easier way to extract this info other than GUI, I would love the advice.
Thank you again.

Mauricio
Contributor III

@Jdrella17 Try this minor change from mm2270 answer

| awk -F  '[<>]' '/<name>/{print $3}'

So the whole code should be

curl -H "Accept: application/xml" -su "username:password" https://your.jamf.pro.url:8443/JSSResource/mobiledeviceconfigurationprofiles" -H "accept: application/xml" | xmllint --format - | awk -F  '[<>]' '/<name>/{print $3}'

Regards

Jdrella17
New Contributor II

Thanks alot @Mauricio . That did exactly what i wanted. I appreciate the help everyone; what a great community we have here.