none
batch - remap printer connections RRS feed

  • General discussion

  • Hi All,

    Hoping someone could help.....

    I need to try and automate the remapping of end user computers print queues from the 'old' to 'new' server. There are 1000s of clients, each with a variety of queues from the old print server.

    Each server has a replica set of queues, achieved simply by using export/import from print management. All queues have been added by end users, they have rights to add/remove queues. Group Policy was not used to deploy.

    What I had hoped to do was to create a simply batch file that would loop through a CSV file with a list of the queues on the 'old' server, then check if any of those exist within the local registry and if so add the same queue on the server and remove the old queue from the old server. Hope that all makes sense!

    I used the following logic;

    @echo off
    
    pushd "\\vir-2016-pcut\printerlist$"
    md "c:\printerlist"
    copy printers.csv "c:\printerlist\"
    popd
    
    SET oldserver=vir-2016-pcut
    SET newserver=vir-2016-dc01
    
    for /f "tokens=1 delims=," %%a in (c:\printerlist\printers.csv) do (
    SET printer=%%a
    )
    
    For /F "Tokens=4 delims=\" %%I In ('reg query HKCU\Printers\Connections ^|find /I ",,%oldserver%,%printer%"') DO rundll32 printui.dll PrintUIEntry /in /q /n\\%newserver%\%printer%
    For /F "Tokens=4 delims=\" %%I In ('reg query HKCU\Printers\Connections ^|find /I ",,%oldserver%,%printer%"') DO rundll32 printui.dll PrintUIEntry /dn /q /n\\%oldserver%\%printer%
    
    rmdir /s /q "c:\printerlist"
    
    :quit

    This works, but only for the last printer within the CSV file. What I've realized is the 'SET printer=%%a' command is simply going through the list sequentially doing as its told, 'setting' each item one after the other before moving on, so if we have 5 queues (but of course there are 100's) then the fifth is the one set.

    I'm not much of a scripting person, hence trying to use batch, but I 'think' what I'm looking for is an array maybe?

    Would really appreciate any help.

    Thanks in advance

    • Changed type Bill_Stewart Monday, March 12, 2018 9:41 PM
    • Moved by Bill_Stewart Monday, March 12, 2018 9:41 PM This is not a free code conversion service
    Monday, February 12, 2018 1:42 PM

All replies

  • Use a GPO to map printers for users rather than trying to script it.

    -- Bill Stewart [Bill_Stewart]

    Monday, February 12, 2018 3:12 PM
  • Hi Bill,

    Thanks for taking the time to reply.

    I'm afraid this isn't really feasible, as with each user/computer having a different set of print queues from one and other, there would be masses of GPOs required to manage this.

    We do use GPOs to deploy our virtual or Pull Print queues, as this is essentially the same queue across all users/computers. Unfortunately its just not realistic to do this with the direct print requirements.

    Further to my email above, I've actually now got a working method, albeit I'm sure the coding is less than optimal. If anyone had any thoughts or advice on how I could refine this I'd be very glad of assistance:

    @echo off
    
    REM Define Variables
    SET oldserver=vir-2012-cas55
    SET newserver=vir-2016-pcut
    SET oldserver2=vir-2012-dre55
    SET newserver2=vir-2016-dc01
    
    REM Create Array
    setlocal enabledelayedexpansion 
    
    set pqueue[0]=PS1-Printer1
    set pqueue[1]=PS2-Printer2
    set pqueue[2]=PS1-Printer3
    set pqueue[3]=PS2-Printer4
    set pqueue[4]=PS1-Printer5
    
    
    
    REM - Loop through local registry and Array, add/remove queues as found
    for /l %%n in (0,1,4) do ( 
      For /F "Tokens=4 delims=\" %%I In ('reg query HKCU\Printers\Connections ^|find /I ",,%oldserver%,!pqueue[%%n]!"') DO (
        rundll32 printui.dll PrintUIEntry /in /q /n"\\%newserver%\!pqueue[%%n]!"
        For /F "Tokens=4 delims=\" %%I In ('reg query HKCU\Printers\Connections ^|find /I ",,%oldserver%,!pqueue[%%n]!"') DO (
    	  rundll32 printui.dll PrintUIEntry /dn /q /n"\\%oldserver%\!pqueue[%%n]!"
        )  
      )
    )
    
    REM SECOND SERVER - Loop through local registry and Array, add/remove queues as found
    for /l %%n in (0,1,4) do ( 
      For /F "Tokens=4 delims=\" %%I In ('reg query HKCU\Printers\Connections ^|find /I ",,%oldserver2%,!pqueue[%%n]!"') DO (
        rundll32 printui.dll PrintUIEntry /in /q /n"\\%newserver2%\!pqueue[%%n]!"
        For /F "Tokens=4 delims=\" %%I In ('reg query HKCU\Printers\Connections ^|find /I ",,%oldserver2%,!pqueue[%%n]!"') DO (
    	  rundll32 printui.dll PrintUIEntry /dn /q /n"\\%oldserver2%\!pqueue[%%n]!"
        )  
      )
    )
    
    :quit

    Although the above code shows 5 objects in an array, in reality there are over 350 queues. As this is read line by line, it takes approx 30 seconds 'per server' to parse through. Its not the end of the world, but I'm sure there is a much better coding approach that how I'm doing it presently.

    Would be great to get the thoughts of people who actually know how to code =D

    Thanks in advance

    Adam

    Monday, February 12, 2018 4:00 PM
  • Unfortunately this forum is not a code review/revision service. We would recommend using PowerShell rather than cmd.exe shell scripts (batch files). Batch files are difficult to read and revise as you have found.

    We would also recommend designing things to take advantage of GPOs. (But this is not the right place to ask about AD design.)


    -- Bill Stewart [Bill_Stewart]

    Monday, February 12, 2018 5:38 PM
  • Hi Bill,

    Sorry to have bothered you.

    Having used this form various times in the past for similar types of support, I've found people to be helpful for those who have made an effort but are stuck through lack of scripting knowledge.

    I didn't come empty handed, believed I had made a fairly good attempt and hoped to get some advice from those who know more.

    Adam

    Monday, February 12, 2018 8:38 PM
  • You will be less stuck if you use PowerShell rather than cmd.exe shell script (batch). PowerShell is far superior to batch in nearly every conceivable way.


    -- Bill Stewart [Bill_Stewart]

    Monday, February 12, 2018 9:10 PM
  • For those who know how to use it, I'm sure it is.

    Thanks

    Adam

    Monday, February 12, 2018 9:16 PM
  • I would recommend starting to learn PowerShell without delay. The learning investment is worth it.

    -- Bill Stewart [Bill_Stewart]

    Monday, February 12, 2018 9:18 PM
  • Appreciate your ongoing support and wisdom.

    Adam

    Monday, February 12, 2018 9:20 PM