Forcing Default Printer

JoshRouthier
Contributor

Been looking through the forums, and I couldn't find anything regarding forcing a default printer for a user (where they couldn't change the default printer). Looking to set a specific printer that is installed on some of our users computers to be the default, and that the user couldn't change the default printer.

I know we could do

lpoptions -d *PrinterName*

but the user could simply just change their default printer to another one installed on their computer after the command has run. I could do a recurring policy that runs once a day or week, but that still gives the user the opportunity to change the default printer between the policy running.

Is this something any does for their environment, and if so, could they share how they achieve this (if it is even possible)?

Many thanks!

3 REPLIES 3

apizz
Valued Contributor

Hey @JoshRouthier !

Interesting because I thought that lpadmin -d PrinterName was what you needed to run to set a default printer, but it does appear despite outputting all the printer options that loptions -d printer works to set and change the default printer.

Below is the script I'm running to set the default printer. You'd probably want an EA to collect the default printer in inventory and scope then use that as criteria in smart group so that way you could target just the machines that lose it using the "Ongoing" frequency & check-in trigger. Just know that if you have spaces in the printer name, the awk '{print $NF}' part of the command below will only get the last part of the printer name, not the whole thing. So, avoid spaces if you can:

#!/bin/bash

PRINTER="PrinterToDefaultTo"
# Get current default printer
DEFAULT=$(/usr/bin/lpstat -d | /usr/bin/awk '{print $NF}')

# If Printer not same as default printer, change it
if [ "$PRINTER" != "$DEFAULT" ]; then
    /bin/echo "Setting default printer to $PRINTER ..."
    /usr/bin/loptions -d "$PRINTER"
else
    /bin/echo "$PRINTER already set to default. Exiting ..."
    exit 0
fi

# Do another check to verify it changed OK
if [ "$(/usr/bin/lpstat -d | /usr/bin/awk '{print $NF}')" = "$PRINTER" ]; then
    /bin/echo "Successfully set default printer to ${PRINTER}!"
    exit 0
else
    /bin/echo "Failed to set default printer to ${PRINTER} ..."
    exit 1
fi

apizz
Valued Contributor

OK, may have spoken too soon. Seems like despite setting a system default location that running it as root does not then change the default for all users or any other user ...

In that case you'd have to check and reset for each user ... honestly though, it appears that if you set a default system printer and a new user logs in it inherits that default printer. So if you really wanted to enforce the default printer, you'd have to check every user's default printer and reset it.

I'm of the mind that if we set it as part of initial config it should be fine and if a user decides to change it for whatever reason, let them.

JRM5513
New Contributor III

Thanks for the script aporlebeke! However, there is a typo in the script, I don't know how it happened, it should be lpoptions and not loptions. I also have edited your script so that it can be deployed through a policy for the connected user.

#!/bin/sh

# Who is connected
loggedInUser=$(python -c 'from SystemConfiguration import SCDynamicStoreCopyConsoleUser; import sys; username = (SCDynamicStoreCopyConsoleUser(None, None, None) or [None])[0]; username = [username,""][username in [u"loginwindow", None, u""]]; sys.stdout.write(username + "
");')
echo "Hello, I am $loggedInUser"

PRINTER="PrinterToDefaultTo"

# Get current default printer
DEFAULT=$(sudo -u $loggedInUser /usr/bin/lpstat -d | /usr/bin/awk '{print $NF}')

# If Printer not same as default printer, change it
if [ "$PRINTER" != "$DEFAULT" ]; then
    /bin/echo "Setting default printer to $PRINTER ..."
    sudo -u $loggedInUser /usr/bin/lpoptions -d "$PRINTER"
else
    /bin/echo "$PRINTER already set to default. Exiting ..."
    exit 0
fi

# Do another check to verify it changed OK
if [ "$(sudo -u $loggedInUser /usr/bin/lpstat -d | /usr/bin/awk '{print $NF}')" = "$PRINTER" ]; then
    /bin/echo "Successfully set default printer to ${PRINTER}!"
    exit 0
else
    /bin/echo "Failed to set default printer to ${PRINTER} ..."
    exit 1
fi