Posted on 05-10-2024 08:38 AM
We're transitioning over to a new printing system and I need to remove printers on all macOS machines. The printers are all named by site, for example Miami HP 504, Dallas HP 653 etc.
What's a good way to delete the printers in bulk by using the site names? I have a script that works for one site at a time. I don't want to just remove all printers as there may be personal printers installed.
Posted on 05-10-2024 08:48 AM
bash arrays would be the answer maybe?
Posted on 05-10-2024 09:48 AM
This may work. I was playing around with using a for loop. It just deleted the printers from my Mac. You would add at list of your printer names to the "printers" variable.
#!/bin/zsh
printers=("Printer1" "Printer2")
for printer in ${printers[@]}; do
/usr/sbin/lpadmin -x "$printer"
done
Posted on 05-10-2024 09:51 AM
yup.. that works and if there are 'a lot' you could always pull in the list from a CSV
Posted on 05-14-2024 07:43 AM
I'm curious how you would do this from a CSV. I think I know how and when I have time I will try it later.
Posted on 05-14-2024 08:10 AM
foo=$(cat path/to/text.csv)
Posted on 05-14-2024 10:21 AM
I ended up doing:
#!/bin/bash
lpstat -a | cut -d " " -f1 | grep "Miami_" | while read MIAMI
do
echo "-- Miami printers detected attempting to delete:" $MIAMI
lpadmin -x $MIAMI
sleep 1
done
and just repeat for each site . . .
Posted on 05-16-2024 01:51 AM
You could potentially make a loop around what you've done there Chris and then use a Here Doc. Here's an example script for Here Doc's
#!/bin/bash
# here_doc_test.bash
SITES=$(cat <<EOF
Miami_
Orlando_
Austin_
Phoenix_
EOF
)
echo $SITES
for i in $SITES
do
echo $i
done