Posted on 12-14-2018 09:14 AM
Hello,
Got an interesting issue where a script which deletes computers from JSS read from txt file via JSS API is working fine when executed from a mac, but not working when executed using a Babun bash shell on Windows Server 2016.
Now, when the file contains only 1 computer name - the script does work, but when several computer names on each string - it just skips through all the computers and does not delete any of them. Based on the output of the script (included the screenshot) it looks like it captures the machine names from txt file correctly, on each string, yet it does not delete them, which is very weird and bizarre.
The script is pretty simple:
#!/bin/bash
# Delete computers from JSS based on the file with the list of computer names
jamfUsername="*********"
jamfPassword="*********"
jamfBaseURL="*********"
jamfComputerResource="JSSResource/computers"
computerList=`cat /file/path/ADtermedmachines.txt`
arr=($computerList)
for i in "${arr[@]}"; do
echo "Deleting computer: $i"
curl -H "Accept: application/xml" -sfku "$jamfUsername:$jamfPassword" "$jamfBaseURL/$jamfComputerResource/name/$i" -X DELETE
done
Anybody has any ideas what may be the issue? As the file is read correctly, the script is working, yet somehow it is not working with multiple computer names... Appreciate any ideas on this!
Posted on 12-14-2018 09:38 AM
To eliminate the possibility that cat'ing a file into your array is the problem, populate the array with simple list of two or three computer names and try again like so:
#!/bin/bash
# Delete computers from JSS based on the file with the list of computer names
jamfUsername="*********"
jamfPassword="*********"
jamfBaseURL="*********"
jamfComputerResource="JSSResource/computers"
# computerList=`cat /file/path/ADtermedmachines.txt`
# arr=($computerList)
arr=(
test-mac-chi1
test-mac-chi2
test-mac-chi
)
for i in "${arr[@]}"; do
echo "Deleting computer: $i"
curl -H "Accept: application/xml" -sfku "$jamfUsername:$jamfPassword" "$jamfBaseURL/$jamfComputerResource/name/$i" -X DELETE
done
If that works, then you might need to do something like the following:
computerList="/file/path/ADtermedmachines.txt"
declare -a arr
# Allows you to comment out lines with '#'
IFS=$'
' arr=("$(grep -v '^#' "$computerList")")
Posted on 12-14-2018 09:59 AM
You might also have to delete the computer using the ID of the device, not the name. In that case you can do this:
#!/bin/bash
# Delete computers from JSS based on the file with the list of computer names
jamfUsername="*********"
jamfPassword="*********"
jamfBaseURL="*********"
jamfComputerResource="JSSResource/computers"
computerList=`cat /file/path/ADtermedmachines.txt`
arr=($computerList)
for i in "${arr[@]}"; do
id=$(curl -H "Accept: application/xml" -sfku "$jamfUsername:$jamfPassword" "$jamfBaseURL/$jamfComputerResource/name/$i" | xmllint --xpath xmllint --xpath '/computer/general/id/text()' - 2>/dev/null)
if [[ -z "$id" ]]; then
echo "Could not determine $i's Jamf Pro ID; skipping."
else
echo "Deleting computer: $i"
curl -H "Accept: application/xml" -sfku "$jamfUsername:$jamfPassword" "$jamfBaseURL/$jamfComputerResource/id/$id" -X DELETE
fi
done
Posted on 12-14-2018 10:42 AM
@ryan.ball , thank you so much for your input!
You were right, the issue is with cat'ing the file into the array. It worked no problem when I simply created an array with machines within the script.
I have tried your approach:
computerList="/home/akamenevadmin/ADtermedmachines.txt"
declare -a arr
# Allows you to comment out lines with '#'
IFS=$'
'
arr=("$(grep -v '^#' "$computerList")")
for i in "${arr[@]}"; do
echo "arr value is: $i"
done
And it does read the file, but it seems like the whole list of computers is captured as 1 "thing".
The output I got with a simple for loop is:
I am having hard time understanding what "grep" is exactly doing here:
# Allows you to comment out lines with '#'
IFS=$'
'
arr=("$(grep -v '^#' "$computerList")")
IFS part is understood, but the ' -v "^#" ', is not, as I understand it should separate out each line into an array (arr), -v - selecting non-matching lines, ^ - character not in the list, should there be a '#' added in a separate line after each computer name? Sorry if I sound confusing, I am not a big expert in bash scripting.
Posted on 12-14-2018 11:33 AM
The grep -v will remove any lines in your list that start with a # symbol so you can comment some things out. I didn't really test out what I send you before, I just pulled it from one of my scripts where I knew I had the same issue during testing.
Does this work for you?
#!/bin/bash
computerList="/home/akamenevadmin/ADtermedmachines.txt"
IFS=$'
'
while read -r i; do
echo "arr value is: $i"
done <<< "$(grep -v '^#' "$computerList")"
Posted on 12-14-2018 12:00 PM
@ryan.ball , ah I see, makes sense. Yep, it is fully working with while loop. Thank you man, would take me a while to figure this out :)