Programmatically Change Printer Settings

The 'Where do you want to go today?' logoImage via Wikipedia

I recently had the need to provide printer admin access on an ERP server to ordinary users. Nothing makes me shudder more than elevating a users access on a machine that holds your entire business operation on it's drives...

I was able to come up with a solution using CPAU.exe which I've written about here.

The scenario: An active label printer and a backup. When one goes down, the other needs to be put into service, but it needs to have the same name as the active one did. The users then also need to be able to "flip a switch" to put the active one back into service.

First, I needed the guts of the switching script, which makes use of Microsoft's prncnfg.vbs script, stored on all Windows boxes in the %windir%\system32 directory (Called ChangePrinter.cmd):

@ECHO OFF

@SET _ONE=ACTIVEPROD
@SET _ONE_DOWN=ACTIVEPROD_146
@SET _TWO=BACKUPPROD
@SET _HOST=BAPFSVR03

:: PRINTERS RECEIVE ADDRESSES VIA DHCP
:: WHEN NO ERROR CONDITIONS EXIST,
:: THE FOLLOWING IS TRUE
:: ACTIVEPROD = 192.168.10.146
:: BACKUPPROD = 192.168.10.145

:: IF AN ERROR CONDITION EXISTS,
:: THESE NAMES WILL BE SWAPPED
:: TO ACCOMODATE INNATRACK

:: FIRST CHECK FOR EXISTENCE OF ACTIVEPROD_146.
:: IF FOUND, RENAME ACTIVEPROD to BACKUPPROD, THEN
:: RENAME ACTIVEPROD_146 TO ACTIVEPROD.


cscript.exe %WINDIR%\system32\prncnfg.vbs -g -s %_HOST% -p %_ONE_DOWN% | FIND /N "Printer name %_ONE_DOWN%"
IF NOT ERRORLEVEL 1 GOTO BACKINSERV


:: IF THE ABOVE FAILS, WE KNOW ALL WE HAVE TO DO
:: IS MOVE ZEBWD1 TO ZEBWD1_146, AND MOVE ZEBWD2
:: UP TO THE #1 SPOT

ECHO REPLACING ACTIVEPROD
cscript.exe %WINDIR%\system32\prncnfg.vbs -x -s %_HOST% -p %_ONE% -z %_ONE_DOWN%
cscript.exe %WINDIR%\system32\prncnfg.vbs -t -s %_HOST% -p %_ONE_DOWN% -h %_ONE_DOWN% -l "Out of service"
cscript.exe %WINDIR%\system32\prncnfg.vbs -x -s %_HOST% -p %_TWO% -z %_ONE%
cscript.exe %WINDIR%\system32\prncnfg.vbs -t -s %_HOST% -p %_ONE% -h %_ONE% -l "In Service"
GOTO OUT


:BACKINSERV
ECHO PUTTING ACTIVEPROD BACK INTO SERVICE
cscript.exe %WINDIR%\system32\prncnfg.vbs -x -s %_HOST% -p %_ONE% -z %_TWO%
cscript.exe %WINDIR%\system32\prncnfg.vbs -t -s %_HOST% -p %_TWO% -h %_TWO% -l "Training - Backup Printer"
cscript.exe %WINDIR%\system32\prncnfg.vbs -x -s %_HOST% -p %_ONE_DOWN% -z %_ONE%
cscript.exe %WINDIR%\system32\prncnfg.vbs -t -s %_HOST% -p %_ONE% -h %_ONE% -l "Production Printer"

:OUT
::EXIT

This is placed in a directory, along with CPAU.exe, and the following script (Called RunChangePrinter.cmd):

@ECHO OFF
cpau.exe -hide -file ChangePrinter.txt -dec -lwp

The above file references ChangePrinter.txt, which was generated by entering the following in a CMD window:

\\myserver\mypath\cpau.exe -u mydomain\myadmin -p %PASSWORD% -ex \\myserver\mypath\ChangePrinter.cmd -file \\myserver\mypath\ChangePrinter.txt -enc

Then simply create a shortcut to RunChangePrinter.cmd and place it where my users can find it!

Reblog this post [with Zemanta]

Comments

Popular posts from this blog

NPI Search Redundancy

freeFTPD

Using ImageMagick and Tesseract to sort TIFFs on Windows