Skip to main content

Is there a way that we can delete printer that user added? we have a open directory that teacher can pick what printer they want to add. now we have new system that only uses one virtual printer, and we already push that one using LPD. what we want to happen is to remove all printer that they add and only printer one printer will stay the one that we push.

Removing all printers from a Mac before adding new ones can be done with this script.



#!/bin/bash

lpstat -p | awk '{print $2}' | while read printer
do
echo "Deleting Printer:" $printer
lpadmin -x $printer
done

@franton how about deleting specific printer like not all the printer only the one that they added using our open directory?


@jamfmdm Well as long as you know the name of it, that's a simple "lpadmin -x printername" on it's own. That assumes the printer isn't in Casper already, otherwise you could use a policy scoped to that machine to remove it.


@franton



Tried this and it work it deleted all the printer



!/bin/bash



lpstat -p | awk '{print $2}' | while read printer
do
echo "Deleting Printer:" $printer
lpadmin -x $printer
done



When i tried this it did not delete the printer that i want



!/bin/bash



lpstat -p | awk '{print $2}' | while read printer
do
echo "Deleting Printer:" UASD-PRINTER-SF-3111-Xerox-4112
lpadmin -x UASD-PRINTER-SF-3111-Xerox-4112
done


@jamfmdm Ok what happens if you just run this on a terminal window?



sudo lpadmin -x UASD-PRINTER-SF-3111-Xerox-4112

@franton This is what happen.




Ok interesting. What's the output from



sudo cat /etc/cups/printers.conf

@franton i got it. it should be GF_Printer_SF_3111_Xerox_4112 for the printer name


Excellent. I had a feeling it was something like that.


@franton hello i have one more question. i made the script something like this



lpadmin -x UASD_PRINTER_GF_Copy_Center_Xerox4112
lpadmin -x UASD_PRINTER_SF_3111_Xerox_4112
lpadmin -x UASD_PRINTER_FF_2108_XeroxPro275
lpadmin -x UASD_PRINTER_SF_312A_HPLJ4014
lpadmin -x UASD_PRINTER_MPH_M12_Xerox5845
lpadmin -x UASD_PRINTER_GF_Copy_Center_SharpColor_MX_5110n



this is the printer we want to remove to all the teacher, but some printers are not installed to teacher laptop and some are installed, but for some reason when i push the policy most of the macbook is failed and this is the error message that is showing.





Yes that will happen if you attempt to remove a printer that doesn't exist on the system. You will have to script around this.


@franton so there is wrong with my script right that why it's not working? but for some are working not sure why.


@franton so there is wrong with my script right that why it's not working? but for some are working not sure why.


It depends entirely on how those printers were originally created on those macs. You've already found that the display name of the printer doesn't always match the CUPS name with the _ instead of - . Hardcoding the printer names will produce uneven results because if they were manually created, then human error during creation will produce different results.



That's why I recommended removing all printers before adding your replacement printer queue.


If you go back to a Mac where your script failed on, and run:



lpstat -p | awk '{print $2}'


That will give you a list of all the CUPS printer names on the Mac. You can then compare to your script. I'm almost certain you will find differences in the names.


hello, i don´t know if this wasn't before, but



lpstat -p | awk '{print $2}'


will output the Queue Name in quotes and that results in not finding that printer when using



#!/bin/bash

lpstat -p | awk '{print $2}' | while read printer
do
echo "Deleting Printer:" $printer
lpadmin -x $printer
done


with Sierra.



Anyone having the same problem?
Anyone with enough awk skills to remove the quotes?


@jensm
We had some „ and “ and used successfully:



lpstat -p | cut -d' ' -f2 | tr -d „ | tr -d “ | xargs -I{} lpadmin -x {}


You can also use wildcards to remove the printers:



printers=($(lpstat -p | awk '{print $2}' | sed '/^$/d'))

for i in "${printers[@]}"
do

if [[ ${i} == *"2831 C9"* ]]; then
lpadmin -x ${i}
elif [[ ${i} == *"2831_C9"* ]]; then
lpadmin -x ${i}
fi

done

Franton... Thank you. Your input on this subject helped me tremendously!


@stevewood, I like what you posted but I'm guessing (because I'm not great at scripting) that your snippet is only looking for 1 instance of the printer name containing "2831 c9". How could I adapt this to loop through and delete all printers with "2831 c9" in their name?


@palmna



Actually, the wildcards before and after each of those should match any printer with that in the name. So, this line:



if [[ ${i} == "2831 C9" ]]; then



Is saying "if the printer name contains 2831 C9, then you may proceed". And this line:



elif [[ ${i} == "2831_C9" ]]; then



is saying "otherwise (else) if the printer name contains 2831_C9, then you may proceed".



The asterisk before and after those names means match anything that contains the value in quotes. If you had an asterisk only at the front it would match names that end with the pattern, and if the asterisk was only at the end it would match names that start with the pattern.



Make sense?


@stevewood Makes sense. Thanks for the explaination.


Follow up to my original answer. If I just want to nuke all printers, i'm now running this:

/System/Library/Frameworks/ApplicationServices.framework/Frameworks/PrintCore.framework/Versions/A/printtool -f --reset

Follow up to my original answer. If I just want to nuke all printers, i'm now running this:

/System/Library/Frameworks/ApplicationServices.framework/Frameworks/PrintCore.framework/Versions/A/printtool -f --reset

@franton - is this the same as Control-Clicking within the Printers section of "Printers & Scanners" and selecting "Reset printing system..."?

 

 


@franton - is this the same as Control-Clicking within the Printers section of "Printers & Scanners" and selecting "Reset printing system..."?

 

 


Precisely that!


Reply