Migration of printe server through vbs

Posted on 2009-02-22
Last Modified: 2012-05-06
Hello I am newbie to vbs and I was wondering if there was a way to migrate a print server to a new one.  Here is the vbscript that i looked up on the internet.  I also use primalscript to create an executable. it appears to work but i am not sure.

'Here is the script:

' put in the server names here (keep the quotes!)
'MovePrinters "mi-mke-mcfile01", "mi-mke-mcprint"

' *********************************************
' Move printers to new server
' *********************************************
Sub MovePrinters(OldServer, NewServer)
' Loops through all network printers and moves all printers
' on "OldServer" to the same printername on "NewServer".
dim WshNetwork, oPrinters, i, PrinterPath, DefaultPrinter, PrinterList

Set WshNetwork = WScript.CreateObject("WScript.Network")
Set PrinterList = CreateObject("Scripting.Dictionary")

' Get the default printer before we start deleting:
DefaultPrinter = GetDefaultPrinter

' Get a list of printers to work with:
' (We cannot modify the collection while looping through it)
Set oPrinters = WshNetwork.EnumPrinterConnections
   For i = 1 to oPrinters.Count Step 2
   If Not PrinterList.Exists(oPrinters.Item(i)) Then
       PrinterList.Add oPrinters.Item(i), "x"
       WScript.Echo "duplicate:  " & oPrinters.Item(i) ' see the duplicate or remove this entire section completely
   End If
Next ' i

' Loop through the printer list and migrate mathching ones:
For Each PrinterPath In PrinterList.Keys

   If StrComp(ServerName(PrinterPath), OldServer, 1) = 0 Then
       WshNetwork.RemovePrinterConnection PrinterPath, True, True
       On Error Resume next
       WshNetwork.AddWindowsPrinterConnection "\\" & NewServer & "\" & _
           If Err.Number = 0 Then
               Set objFile = wscript.CreateObject("Scripting.FileSystemObject")
               If Not objFile.FolderExists("c:\printers_remapped") Then
               objFile.CreateFolder "c:\printers_remapped"
               End If
           End If    
           'If Err.Number = -2147023095 Then
           ' MsgBox "The printer """ & ShareName(PrinterPath) & _
           ' """ does not exist on server """ & NewServer & """." & vbCrLf & _
           ' "The printer has been removed.", vbOKonly + vbExclamation, _
           ' "Missing printer"
           'End If
       On Error goto 0
   End If

'Set the default printer:
If ServerName(DefaultPrinter) = OldServer Then
   On Error Resume Next
   WshNetwork.SetDefaultPrinter "\\" & NewServer & "\" & _
   'If Err.Number = -2147352567 Then
   'MsgBox "Your default printer did not exist, and has been deleted.", _
   ' vbOKonly + vbInformation, "Invalid default printer"
   'End If
   On Error goto 0
End If
End Sub ' MovePrinters

Function GetDefaultPrinter()
' Returns the UNC path to the current default printer
Dim oShell, sRegVal, sDefault
Set oShell = CreateObject("WScript.Shell")
sRegVal = _
   "HKCU\Software\Microsoft\Windows NT\CurrentVersion\Windows\Device"
sDefault = ""
On Error Resume Next
sDefault = oShell.RegRead(sRegVal)
sDefault = Left(sDefault ,InStr(sDefault, ",") - 1)
On Error Goto 0
GetDefaultPrinter = sDefault
End Function

Function ServerName(sPrinterPath)
Dim aPrinterPath
ServerName = ""
If Left(sPrinterPath, 2) = "\\" Then
   aPrinterPath = Split(sPrinterPath, "\")
   ServerName = aPrinterPath(2)
End If
End Function

Function ShareName(sPrinterPath)
Dim aPrinterPath
ShareName = ""
   If Left(sPrinterPath, 2) = "\\" Then
   aPrinterPath = Split(sPrinterPath, "\")
   ShareName = aPrinterPath(3)
End If
End Function
any help would be welcome
erwin miranda
Question by:erwin_miranda
    LVL 3

    Accepted Solution

    That's one that works..  
    Another is below.  I'm actually relatively new to vbs as well, but these both seem to do the same thing.  The below one seems simpler though.. less steps.
    Dim from_sv, to_sv, PrinterPath, PrinterName, DefaultPrinterName, DefaultPrinter
    Dim DefaultPrinterServer, SetDefault, key
    Dim spoint, Loop_Counter
    Dim WshNet, WshShell
    Dim WS_Printers
    DefaultPrinterName = ""
    spoint = 0
    SetDefault = 0
    set WshShell = CreateObject("")
    from_sv = "\\mi-mke-mcfile01" 'This should be the name of the old server.
    to_sv = "\\mi-mke-mcprint" 'This should be the name of your new server.
    'Make sure there are printers installed
    On Error Resume Next
    key = "HKCU\Software\Microsoft\Windows NT\CurrentVersion\Windows\Device"
    DefaultPrinter = LCase(WshShell.RegRead (key))
    If Err.Number <> 0 Then
       DefaultPrinterName = ""
    'If the registry read was successful then parse out the printer name so we can
    ' compare it with each printer later and reset the correct default printer
    ' if one of them matches this one read from the registry.
    spoint = instr(3,DefaultPrinter,"\")+1
    DefaultPrinterServer = left(DefaultPrinter,spoint-2)
    if DefaultPrinterServer = from_sv then
       DefaultPrinterName = mid(DefaultPrinter,spoint,len(DefaultPrinter)-spoint+1)
    end if
    end if
    Set WshNet = CreateObject("WScript.Network")
    Set WS_Printers = WshNet.EnumPrinterConnections
    For Loop_Counter = 0 To WS_Printers.Count - 1 Step 2
       PrinterPath = lcase(WS_Printers(Loop_Counter + 1))
       if LEFT(PrinterPath,len(from_sv)) = from_sv then
          spoint = instr(3,PrinterPath,"\")+1
          PrinterName = mid(PrinterPath,spoint,len(PrinterPath)-spoint+1)
          WshNet.RemovePrinterConnection from_sv+"\"+PrinterName
          WshNet.AddWindowsPrinterConnection to_sv+"\"+PrinterName
          if DefaultPrinterName = PrinterName then
             WshNet.SetDefaultPrinter to_sv+"\"+PrinterName
          end if
       end if
    Set WS_Printers = Nothing
    Set WshNet = Nothing
    Set WshShell = Nothing

    Open in new window


    Author Closing Comment

    Thanks for the Help man..

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    Introduction During my participation as a VBScript contributor at Experts Exchange, one of the most common questions I come across is this: "I have a script that runs against only one computer. How can I make it run against a list of computers in …
    If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now