VBS Script: Migrate granularly users from one printer server to another

The below script is an example of how you can migrate users from one printer server to another based on the printer model or name.

Sometimes doing a big bang migratiion from an old print server to a new one is not the best idea so this script allows you to provide a list of printer models to migrate rather than doing them all at once. When I use this script I call it from a logn script and add a model or two a day until the migration is complete.

The printers will need to be already setup, shared and tested on your new server this script will just move the users connection from one server to another.

The script will also preserve the users default printer.

If you would prefer a big bang approach take a look at this script .

This script could be easily changed to lookup from a list of share names rather than a list of drivers, please comment if you would like help with that

on error resume nextSet objWMIService = GetObject("winmgmts:\\.\root\cimv2")
Set colPrinters = objWMIService.ExecQuery ("Select * From Win32_Printer Where Local = False")
Set objMigrationList = CreateObject("scripting.dictionary")
Set objNetwork = CreateObject("WScript.Network")
printServerOld = "\\PRINTSRV01" 'The server to move from MUST IN IN CAPS
printServerNew = "\\PRINTSRV02" 'The server to move to MUST IN IN CAPS

' A list of models to migrate using the exact driver name as it appears in
' the Advanced tab of the printer MUST BE IN CAPS
objMigrationList.Add "BROTHER HL-4040CN SERIES", "MIGRATE"
objMigrationList.Add "BROTHER HL-5250DN SERIES", "MIGRATE"

If colPrinters.Count <> 0 Then 'If there are some network printers
For Each objPrinterInstalled In colPrinters ' For each network printer
currentPrintServer = ""
currentShareName = ""
currentDriverName = ""
currentIsDefault = False

currentPrintServer = ucase(objPrinterInstalled.ServerName) ' Get the server name
currentShareName = ucase(objPrinterInstalled.ShareName) ' Get the share name
currentDriverName = ucase(objPrinterInstalled.DriverName) ' Get the driver name \ model
currentIsDefault = objPrinterInstalled.Default

' If the current printer is connectd to the old server and if the driver is listed in
' objMigrationList then migrate the printer from the old server to the new
if currentPrintServer = printServerOld and objMigrationList.exists(currentDriverName) then
MigratePrinter currentPrintServer & "\" & currentShareName, printServerNew & "\" & currentShareName

if currentIsDefault = TRUE then
SetDefaultPrinter  currentShareName
End if
end if
Next
End if

Sub MigratePrinter(OldPrinter, NewPrinter)
objNetwork.RemovePrinterConnection OldPrinter ' Remove from old
objNetwork.AddWindowsPrinterConnection NewPrinter 'Add on new
End sub

Sub SetDefaultPrinter(ShareName)
Set colDefaultPrinter =  objWMIService.ExecQuery ("Select * from Win32_Printer Where ServerName = " & printServerNew & " and ShareName = " & ShareName & "")

For Each objDefaultPrinter in colDefaultPrinter
objDefaultPrinter.SetDefaultPrinter()
Next
End sub

 

3 thoughts on “VBS Script: Migrate granularly users from one printer server to another”

  1. Hello,

    Thank you for posting this script. I do need help. Is there a way for the script to find any printers that are point to ors-13ptr1, ors-10ptr1 and then pointing them to ors-ptr?

    I am migrating users frm XP to Win 7 and trying to remapped their old network printers to the new 2008 R2 printer server.

    Any help would be greatly appreciated.

     

    Thank you,

     

    ~Carlo

    Reply
  2. Hi and welcome,

    I think the below should do what you are after?

    Let me know if you need another help

    on error resume next

    Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
    Set colPrinters = objWMIService.ExecQuery ("Select * From Win32_Printer Where Local = False")
    Set objMigrationList = CreateObject("scripting.dictionary")
    Set objNetwork = CreateObject("WScript.Network")

    printServerOld1 = "\\ORS-13PTR1" 'The server to move from MUST IN IN CAPS
    printServerOld2 = "\\ORS-10PTR1" 'The server to move from MUST IN IN CAPS

    printServerNew = "\\ORS-PTR" 'The server to move to MUST IN IN CAPS

    If colPrinters.Count <> 0 Then 'If there are some network printers
           For Each objPrinterInstalled In colPrinters ' For each network printer
                   currentPrintServer = ""
                    currentShareName = ""
                    currentIsDefault = False
                   
                    currentPrintServer = ucase(objPrinterInstalled.ServerName) ' Get the server name
                   currentShareName = ucase(objPrinterInstalled.ShareName) ' Get the share name
                   currentIsDefault = objPrinterInstalled.Default
                   
                    ' If the current printer is connectd to the either of the old servers
                   ' then migrate the printer from the old server to the new
                   if currentPrintServer = printServerOld1 or currentPrintServer = printServerOld2 then
                            MigratePrinter currentPrintServer & "\" & currentShareName, printServerNew & "\" & currentShareName
                           
                            if currentIsDefault = TRUE then
                                    SetDefaultPrinter  currentShareName
                            End if
                    end if
            Next
    End if

    Sub MigratePrinter(OldPrinter, NewPrinter)
            objNetwork.RemovePrinterConnection OldPrinter ' Remove from old
           objNetwork.AddWindowsPrinterConnection NewPrinter 'Add on new
    End sub

    Sub SetDefaultPrinter(ShareName)
            Set colDefaultPrinter =  objWMIService.ExecQuery ("Select * from Win32_Printer Where ServerName = " & printServerNew & " and ShareName = " & ShareName & "")
           
            For Each objDefaultPrinter in colDefaultPrinter
                    objDefaultPrinter.SetDefaultPrinter()
            Next
    End sub
     

    Reply
  3. Hi Justin.

    Thanks a lot for your post here! We got *exactly* the same situation here. Just a few minutes ago, my IT department called me that they are flooded with SNMP requests… *outch*

    Thanks to your article, we could resolve the issue.

    Really strange that it is configured by default that way… >cartuchos tinta

    Cheers,
    Martin.

    Reply

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.