Problem Pulling Computer ID with JAMF API

Hugonaut
Valued Contributor II

So I am using the API calls to Pull a computer ID. Here is the API Page with the Computer information.

JSSURL:8443/JSSResource/computers/serialnumber/#########/subset/general

-----<computer> --<general> <id>3258</id> <name>Hugonaut</name> <mac_address>##:##:##:##:##</mac_address> <alt_mac_address>##:##:##:##:##</alt_mac_address> <ip_address>#.#.#.#</ip_address> <last_reported_ip>#.#.#.#</last_reported_ip> <serial_number>########</serial_number> <udid>########</udid> <jamf_version>10.#</jamf_version> <platform>Mac</platform> <barcode_1/><barcode_2/> <asset_tag></asset_tag> <remote_management></remote_management> <mdm_capable>true</mdm_capable> <mdm_capable_users/> <report_date></report_date> <report_date_epoch></report_date_epoch> <report_date_utc>#</report_date_utc> <last_contact_time>2018-11-07 10:47:34</last_contact_time><last_contact_time_epoch>1541605654628</last_contact_time_epoch><last_contact_time_utc>2018-11-07T10:47:34.628-0500</last_contact_time_utc><initial_entry_date>2018-08-30</initial_entry_date><initial_entry_date_epoch>1535638796574</initial_entry_date_epoch><initial_entry_date_utc>2018-08-30T10:19:56.574-0400</initial_entry_date_utc><last_cloud_backup_date_epoch>0</last_cloud_backup_date_epoch><last_cloud_backup_date_u/> <last_enrolled_date_epoch>1541603743286</last_enrolled_date_epoch><last_enrolled_date_utc>2018-11-07T10:15:43.286-0500</last_enrolled_date_utc><distribution_point/> <sus/> <netboot_server/> ---<site> <id>-1</id> <name>None</name> </site> <itunes_store_account_is_active>false</itunes_store_account_is_active> </general> </computer>

The problem is when running this bash command it is pulling both the Computer/General ID & the Site ID. I've bolded both. So instead of an output of just 3258, I am getting an output of 3258 -1

Of course I can work around, but how do I get the straight output? What am I doing wrong?

JAMF_ALLCOMPUTERID=$(curl -H "Accept: text/xml" -sfku "${apiuser}:${apipass}" "${jssURL}/JSSResource/computers/serialnumber/${JAMF_SERIAL}/subset/general" | xmllint --format - 2>/dev/null | awk -F'>|<' '/<id>/{print $3}')

example of my workaround, but this just leads to making a bunch of if 1 character else if 2 characters, else if 3 characters, etc and don't want to do that, I need it to work without a workaround.

JAMF_COMPUTERID=${JAMF_ALLCOMPUTERID:0:4}
________________
Looking for a Jamf Managed Service Provider? Look no further than Rocketman
________________


Virtual MacAdmins Monthly Meetup - First Friday, Every Month
1 ACCEPTED SOLUTION

mm2270
Legendary Contributor III

Hi there. You have a few options. One is to tell awk to stop printing results as soon as it finds one. You do that by adding an exit to the command like so - awk -F'>|<' '/<id>/{print $3; exit}'
That will only print the id from the computer record and not the Site one. But this only works because the computer ID comes first in the API output. If it was in a different order, that wouldn't help you.

Aside from that, you should learn to use xpath as it can often be better at drilling down to the exact result from the API that you want. Instead of the xmllint and on after the API call, add this: | xpath '/computer/general/id/text()'

So your full command would look like this instead:

JAMF_ALLCOMPUTERID=$(curl -H "Accept: text/xml" -sfku "${apiuser}:${apipass}" "${jssURL}/JSSResource/computers/serialnumber/${JAMF_SERIAL}/subset/general" | xpath '/computer/general/id/text()'

View solution in original post

3 REPLIES 3

mm2270
Legendary Contributor III

Hi there. You have a few options. One is to tell awk to stop printing results as soon as it finds one. You do that by adding an exit to the command like so - awk -F'>|<' '/<id>/{print $3; exit}'
That will only print the id from the computer record and not the Site one. But this only works because the computer ID comes first in the API output. If it was in a different order, that wouldn't help you.

Aside from that, you should learn to use xpath as it can often be better at drilling down to the exact result from the API that you want. Instead of the xmllint and on after the API call, add this: | xpath '/computer/general/id/text()'

So your full command would look like this instead:

JAMF_ALLCOMPUTERID=$(curl -H "Accept: text/xml" -sfku "${apiuser}:${apipass}" "${jssURL}/JSSResource/computers/serialnumber/${JAMF_SERIAL}/subset/general" | xpath '/computer/general/id/text()'

Hello, mm2270 you are the man, for sure. And thank you for sharing your explanation and work. I tested your command as I needed to grab the computer ID for reset device app I am testing the build for. 

In my command I used the "-e" added to XPath and I had success:

JAMF_ALLCOMPUTERID=$(curl -H "Accept: text/xml" -sfku "${apiuser}:${apipass}" "${jssURL}/JSSResource/computers/serialnumber/${JAMF_SERIAL}/subset/general" | xpath -e '/computer/general/id/text()')

With out the -e I was receiving this error in my test:

Usage:
/usr/bin/xpath5.30 [options] -e query [-e query...] [filename...]

If no filenames are given, supply XML on STDIN. You must provide at
least one query. Each supplementary query is done in order, the
previous query giving the context of the next one.

Options:

-q quiet, only output the resulting PATH.
-s suffix, use suffix instead of linefeed.
-p postfix, use prefix instead of nothing.
-n Don't use an external DTD.

Not sure if this is correct but I wanted to share if it can help you or anyone else.

Dom

Hugonaut
Valued Contributor II

Thank you very much @mm2270 , im sitting here reading the awk man page trying to use the split command and other sorts on nonsense commands when all i needed to do was add an exit.... Learn something new every day!

Huge Help, Really appreciate it! Will look into converting everything I have over to xcode as well.

________________
Looking for a Jamf Managed Service Provider? Look no further than Rocketman
________________


Virtual MacAdmins Monthly Meetup - First Friday, Every Month