Batch Script to query Printer network name and change to another server

Go To StackoverFlow.com

1

I have 6 Network Printers on a server and need to move them to another server. I have 95 clients that use them but some only have certain printers installed. I need to have a batch file query for a printer, if it exist then remove and re-add it to the new server. Here is the script so far. I can't figure out the query statement.

@ Remove current printer from computer
Rundll32 PrintUI.DLL,PrintUIEntry /dn /q /n \\server6\Printer1

@ Add new Printer to computer
Rundll32 PrintUI.DLL,PrintUIEntry /in /q /n \\server11\Printer1

@ Remove current printer from computer
Rundll32 PrintUI.DLL,PrintUIEntry /dn /q /n \\server6\Printer2

@ Add new Printer to computer
Rundll32 PrintUI.DLL,PrintUIEntry /in /q /n \\server11\Printer2
2012-04-05 16:43
by user1011061


1

Here is something that should generate the output you need. That is the location of the prnmngr.vbs in Windows 7, I think it's in the root of System32 in XP. either way, it should remove the old and add the new now.

@echo off
setlocal
cscript %windir%\System32\Printing_Admin_Scripts\en-US\prnmngr.vbs -l > printers.txt
set count=0
FOR /F "tokens=1-3 delims= " %%G IN (printers.txt) DO call :loop %%G %%H %%I
:loop
if "%1"=="" goto :endloop
if "%1"=="Printer" (
    if "%2"=="name" (
            if "%3"=="\\server6\Printer1" (
            echo Printer1
            REM Remove current printer from computer
            Rundll32 PrintUI.DLL,PrintUIEntry /dn /q /n \\server6\Printer1
            REM Add new Printer to computer
            Rundll32 PrintUI.DLL,PrintUIEntry /in /q /n \\server11\Printer1
        )
        if "%3"=="\\server6\Printer2" (
            echo Printer2
            REM Remove current printer from computer
            Rundll32 PrintUI.DLL,PrintUIEntry /dn /q /n \\server6\Printer2
            REM Add new Printer to computer
            Rundll32 PrintUI.DLL,PrintUIEntry /in /q /n \\server11\Printer2
        )
        pause
    )
)
SHIFT
goto :loop
:endloop

Edit made to include your Remove/Add functions based on printer name match.

2012-04-06 19:46
by iesou
I tested it and it creates the text file with printer names. How do I have the script read through that so if \server6\printer1 exists then remove and re-add \server11\printer - user1011061 2012-04-09 13:28
I edited the above script. it no longer generates a text file, instead it stores the printer name as a variable and tests that variable against your known printer name. If it finds a known printer it will remove it and add the new one. Also... if this works for you, if you could upvote and click on the check next to the answer I'd appreciate it - iesou 2012-04-09 14:29
Thank you for all the help. There is still one problem. Now it will remove the old printer and add the new one but if the old printer 2 does not exist on a computer it will still add the new printer 2 to the syste - user1011061 2012-04-09 15:36
Hmm... I made one more change here, should help figure out what's going on. I took out the printer variable... that was unnecissary, and then also added a pause to help you better see what options it was using on each run through the loop. I ran a test on my machine changing \server6\printer1 to fax because I have a virtual fax printer and left \server6\printer2 on for the second if and it echo'd only fax and not printer2, hopefully this works for you - iesou 2012-04-09 16:49
It doesn't echo anything. It just says "Press any key to continue" three times before disappearing. Also, It doesn't add or remove printers now. Sorry for being a pain - user1011061 2012-04-09 17:12
I know this might sound dumb... but the \server6\ printers are still installed on this machine? because when you tell me it just says "Press any key to continue" 3 times, what it is doing is finding 3 printers but not finding either server6 printer. If you do have the server6 printers still installed on this machine I would doublecheck the IF criteria in if "%3"=="\server6\Printer1" and if "%3"=="\server6\Printer2".

The only reason I say that is because I know this code works on my machine and I have a XP VM I ran it on for kicks as well - iesou 2012-04-09 17:25

Ads