Looping Through CSV for Variables - Errors

ferriterj1
New Contributor III

Hello!

I'm trying to loop through a CSV of device serial numbers, extract those serial numbers as variables, use the variables and the API to get the JSS ID of the mobile devices as another variable, then use this new variable to execute some remote commands on the devices :

#!/bin/sh
while IFS="," read serial; do

    echo $serial

    jID=$(curl -sku $apiUN:$apiPW -H "Accept: text/xml" $apiURL/JSSResource/mobiledevices/serialnumber/$serial | xmllint --xpath '/mobile_device/general/id/text()' -)

    curl -sku $apiUN:$apiPW -H "Accept: text/xml" $apiURL/JSSResource/mobiledevicecommands/command/RestartDevice/id/$jID -X POST

    done < ~/Desktop/TestFile.csv

However, whenever I run the script get the following errors :

-:1: parser error : Document is empty

My CSV is a .csv created in excel with a single column of serial numbers and no header. Is it possible that there is an error there? Or can I not loop and use these variables in the csv? The actual command I will be doing for this will be the UnmanageDevice command. The RestartDevice is just to use for testing.

I'm not too well versed with scripting yet and any help would be appreciated!
If there is a better way for me to do this then I am all for it!

1 ACCEPTED SOLUTION

cdenesha
Valued Contributor III

Hello,

As an alternative, you could use The MUT to do your API work for you. It works by reading in a CSV of either serial numbers or JSS IDs and doing the specified work in the other settings. So for example, you could create an empty Static Group, use The MUT to ADD the serials to the static group, and then run your UnManage Devices command as an Action on the Static Group in Jamf Pro. And because only Managed devices are in a Static Group it is self-cleaning.

chris

View solution in original post

6 REPLIES 6

cdenesha
Valued Contributor III

Hello,

As an alternative, you could use The MUT to do your API work for you. It works by reading in a CSV of either serial numbers or JSS IDs and doing the specified work in the other settings. So for example, you could create an empty Static Group, use The MUT to ADD the serials to the static group, and then run your UnManage Devices command as an Action on the Static Group in Jamf Pro. And because only Managed devices are in a Static Group it is self-cleaning.

chris

ferriterj1
New Contributor III

I do like that approach with MUT and more than likely will end up going that route.

Do you have any idea why I would be getting that error with my original script? Now that there is an easier solution provided I'm curious as to why I was failing in the beginning.

Thanks for your response @cdenesha !

sdagley
Esteemed Contributor II

@ferriterj1 If your .csv is a file with 1 column of numbers then a , is not what you want to be using as your IFS

ferriterj1
New Contributor III

@sdagley what should i be using then? Thank you for the response! Trying to learn as I go!

sdagley
Esteemed Contributor II

@ferriterj1 If the only thing on a line in your file is a serial number simply while read serial; do should work. You will need a blank line at the end of the file.

lassekivikas
New Contributor II

I am in the same situation currently, with my script looking pretty similar, only that I user the names of the computer:

#!/bin/bash

username=xxx
password=xxx

input="/path/to/file.csv"
while IFS= read -r line; do
    #echo $line
    curl -sku $username:$password -X GET -H "Accept: application/xml" https://jamfprourl/JSSResource/computers/name/$line | xmllint --xpath "/computer/general/name/text()" -
done < "$input"

As well, I am getting the "parser error : Document is empty" error message, in every line that is in the .csv. If I user the echo in there, it will echo everything, but after that every echo the parser error comes after.

What I'm trying to do, is to update building for all the computers in the list. The reason I have the GET in there is that I wanted to test this before updating everything "by accident". The names of the computer contain "-" in the middle, but I think this shouldn't affect becauseI tried to replace the $line in the curl part with the name and it is working normally.

I also tried what @sdagley suggested, but it didn't help.
I'd appreciate the help, I know about the MUT but this little thing is bothering me.