Posted on 10-19-2020 04:42 AM
Hey,
Need some help with a script I'm making to automate bulk removal of computers using the API.
It all seems to work fine, it loops through a file grabbing a serial off each line, all the variables do what they should. But, the URL always flags as "curl: (3) URL using bad/illegal format or missing URL" even though the URL thats outputted works fine if I copy and paste it in to a browser.
#!/bin/sh
while IFS= read -r line
do
echo "serial to delete is $line" >> /Log/Path/Deleted.txt
curl -u "USERNAME:PASSWORD" -X DELETE "https://COMPANY.jamfcloud.com/JSSResource/computers/serialnumber/$line" >> /Log/Path/Deleted.txt
echo "https://COMPANY.jamfcloud.com/JSSResource/computers/serialnumber/$line" >> /Log/Path/Deleted.txt
echo "DELETED" >> /Log/Path/Deleted.txt
done < "$file"
I've tried the URL without "", tried ${line}, "$line", "${line}". Tried without the output after the URL incase that was messing it up.
Can't figure out whats going wrong?!
Posted on 10-20-2020 12:09 AM
@perryd84 You may need to add the --header 'Content-Type: text/xml' into the curl command.
We are assuming the source "$file" is good and valid, right?
Just found a script I've created to delete a bunch of policies a while back.
deletePolicy() {
policyID="$1"
curlOutput=$(curl --write-out '%{http_code}' --output /dev/null --silent "$server/policies/id/$policyID" --user "$user:$pass" --header "Content-Type: text/xml" --request DELETE)
if [ $curlOutput -eq 200 ]; then
echo "DELETE: SUCCESS"
else
echo "DELETE: FAIL ($curlOutput)"
fi
}
policyIDs=( 739 751 613 999 1027 1007 305 436 1152 469 268 614 622 875 1151 508 1150 )
for thisPolicy in "${policyIDs[@]}"
do
echo "Processing policy ID: $thisPolicy"
deletePolicy $thisPolicy
done
--write-out '%{http_code} will give the curl result code (200 is good, done).
--output is going to /dev/null but you could output it to a file and if you would like more info for your troubleshooting replace '--silent' with '--verbose'
Regards
Posted on 10-20-2020 02:48 AM
Thanks for that!
I'll throw in that CURL check see if that helps. Cheers!
Posted on 10-20-2020 06:10 AM
So, I managed to fix it myself. I re-wrote it using an array with a for loop instead of a while loop.
Works really nice! I created a self service item to run it. You get a nice pop up asking for the file full of serials, just drag and drop your file in and it does the rest!
Heres the code if anyone is interested. Obviously edit the CAPITALS for your own companies.
#!/bin/bash
#
# Created by Perry 19/10/2020
#
# Script to bulk remove computers from JAMF API
#
#################################################################
# Todays date
today=$(date +%d%m%y)
echo "**** Date of Deletion $today ****" >> /PATH/TO/LOG/FILE.txt
# Specify File containing Serial numbers
file=$(osascript -e 'Tell application "System Events" to display dialog "Please enter the file path of the list of serials.
You can drag and drop the file or enter it manaully.
Example: /USER/Documents/SERIAL NUMBERS.csv" with title "COMPANY Bulk Device removal" with icon alias "YOUR:ICON:HERE" default answer ""' -e 'text returned of result' 2>/dev/null)
# Create array from file
serials=$(cat "$file" | tr '
' ' ')
declare -a array=($serials)
# Loop through serial numbers in array to delete from JAMF
for serial in ${array[@]}
do
echo "serial to delete is $serial" >> /PATH/TO/LOG/FILE.txt
curl -u "USERNAME:PASSWORD" -X DELETE "https://COMPANY.jamfcloud.com/JSSResource/computers/serialnumber/$serial" >> /PATH/TO/LOG/FILE.txt
echo "DELETED" >> /PATH/TO/LOG/FILE.txt
done