Refresh Printers

jalbert
Contributor

Our teachers have automatically installed printers when they change network location, login or check in, this happens once a day.

Just in case something gets screwy with the printers and they have to reinstall, I wanted to have a script that they can run in Self Service to refresh the printers.

I run the following script (which will delete all printers, even ones that they manually configured);

#!/bin/bash

#Deletes all printers installed, even non-district printers.
#





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

But since the triggers I have on the printer install policies only trigger once a day, how can I do another check in ignoring that they have already checked in today.

I tried to issue a jamf recon, but that didn't work.

Any ideas?

1 ACCEPTED SOLUTION

ryan_ball
Valued Contributor

@jcalvert I have a script that will install printers only if no printers are installed. This could be executed pretty frequently as it only takes action if no printers are installed.

You would need to supply parameter 4 of this script with either "install" to just install printers no matter what, or "onlyifempty" to trigger the printer installation only if there are no printers installed.

You would also need to create a policy that will install printers using only a custom trigger of "installPrinters" and set parameter 5 of this script to "installPrinters".

#!/bin/bash

action="$4"     # (install|onlyifempty)
trigger="$5"    # (installprinters)
printerCount=$(/usr/bin/lpstat -v | wc -l)

function install_printers () {
    echo "Triggering policy to install printers..."
    if /usr/local/jamf/bin/jamf policy -event "$trigger" > /dev/null ; then
        echo "Completed policy to install printers."
    else
        echo "An error occured running the policy; exiting."
        exit 1
    fi
}

if [[ "$action" == "install" ]]; then
    install_printers
elif [[ "$action" == "onlyifempty" ]]; then
    if [[ "$printerCount" -lt "1" ]]; then
        install_printers
    else
        echo "A printer already exists on this Mac; not installing printers."
    fi
else
    echo "Invalid action "$action" specified; exiting."
    exit 1
fi

exit 0

View solution in original post

5 REPLIES 5

jalbert
Contributor

To clarify why we only do it once a day, we didn't want the printers constantly installing, unless you don't see an issue with that.

Or if you can think of a way to check to see if the printer is already installed, then skip it. We install the printer through the printer policy payload, not via script.

ryan_ball
Valued Contributor

@jcalvert I have a script that will install printers only if no printers are installed. This could be executed pretty frequently as it only takes action if no printers are installed.

You would need to supply parameter 4 of this script with either "install" to just install printers no matter what, or "onlyifempty" to trigger the printer installation only if there are no printers installed.

You would also need to create a policy that will install printers using only a custom trigger of "installPrinters" and set parameter 5 of this script to "installPrinters".

#!/bin/bash

action="$4"     # (install|onlyifempty)
trigger="$5"    # (installprinters)
printerCount=$(/usr/bin/lpstat -v | wc -l)

function install_printers () {
    echo "Triggering policy to install printers..."
    if /usr/local/jamf/bin/jamf policy -event "$trigger" > /dev/null ; then
        echo "Completed policy to install printers."
    else
        echo "An error occured running the policy; exiting."
        exit 1
    fi
}

if [[ "$action" == "install" ]]; then
    install_printers
elif [[ "$action" == "onlyifempty" ]]; then
    if [[ "$printerCount" -lt "1" ]]; then
        install_printers
    else
        echo "A printer already exists on this Mac; not installing printers."
    fi
else
    echo "Invalid action "$action" specified; exiting."
    exit 1
fi

exit 0

jalbert
Contributor

@ryan.ball Thanks I will take a look at this, it looks like I could probably make it work.

ShaunRMiller83
Contributor III

This isn't the same exact ask but I have a similar script that will:

1) Check the current printers and remove them, 2) Install print drivers
3) Reinstall the printers

#!/bin/sh

##Collect existing printers
    existingprinters=($(lpstat -p | awk '{print $2}' > /tmp/existingprinters.txt))

##Delete existing printers
    IFS=$'
'
        declare -a existingprinters=($(lpstat -p | awk '{print $2}'))
    unset IFS

    for i in "${existingprinters[@]}"
    do
        echo Deleting $i
        lpadmin -x $i
    done

##Remove existing print drivers
    rm -rf /Library/Printers/*
    sleep 15

##Install print drivers
    jamf policy -trigger printdriver

# Reinstall Printers

    IFS=$'
'
        declare -a printerstoinstall=($(cat /tmp/existingprinters.txt))
    unset IFS

    for i in "${printerstoinstall[@]}"
        do
            echo Installing $i
            jamf policy -trigger $i
        done

##Clean up log file
    rm -rf /tmp/existingprinters.txt

jalbert
Contributor

Thanks @shaunrmiller83 I will take a look at that too