VB script to change workstation printer mapping after new print server built - having problems

Posted on 2011-10-27
Last Modified: 2012-08-13
We currently have 2 print servers setup and some of the printers are installed on both servers with different sharenames. The old server will be decommissioned once all users are migrated over. I needed to move the users onto the NEW print server.  Most of them only have a few printers mapped.
I know extremely little about VBScript but found a nice one from an older EE post (from RobSampson as a matter of fact) that uses an array that updates mappings if they are found on the computer.
On Windows XP the script seems to work great accept it does not re-map this printer:
"\\ricvc1\HP Color LaserJet CP2020 Series PCL 6:\\c0prntsvr1v\REGPHARMLEXX790", _

My assumption is maybe it is to long or the spaces are throwing it off?

My second problem is that it does not work in Windows 7 at all. I do not know what would have to be changed for it to work on Win 7 as well. Would there need to be an initial script that looks to see if it is Win XP or Win 7, then run a second script specific to that OS?

Any help would be great as my boss knows I have no prior script experience, yet he put it in my to-do list. :-D

Thanks in advance!
strComputer = "."
strLogFileShare = "\\c0fs1\ricvc1_printer_changes\userlogins\"

Set objFSO = CreateObject("Scripting.FileSystemObject")
Const intForAppending = 8
Set objShell = CreateObject("WScript.Shell")
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") 
arrPrinterShares = Array( _
      "\\ricvc1\REGACTCAN2035:\\c0prntsvr1v\REGACCTLEX658", _
      "\\ricvc1\REGMRHPLJ4250TN2:\\c0prntsvr1v\REGMRHPLJ4250TN2", _
      "\\ricvc1\REGMRHPLJ4250TN:\\c0prntsvr1v\REGMRHPLJ4250TN", _
      "\\ricvc1\REGOHDHPLJ4015TN:\\c0prntsvr1v\REGOHDHPLJ4015TN", _
      "\\ricvc1\REGADMHPLJ2430DTN:\\c0prntsvr1v\REGADMHPLJ2430DTN", _
      "\\ricvc1\REGREFHP3015:\\c0prntsvr1v\REGREFHP3015", _
      "\\ricvc1\REGOHDHPLJ2300L:\\c0prntsvr1v\REGOHDHPLJ2300L", _
      "\\ricvc1\REGACTHPLJ4000TN:\\c0prntsvr1v\REGACTHPLJ4000TN", _
      "\\ricvc1\REGPAHPLJ4350TN:\\c0prntsvr1v\REGPAHP4015", _
      "\\ricvc1\REGLGLHP3005:\\c0prntsvr1v\REGLGLHP3005", _
      "\\ricvc1\REGFACHPLJ4350TN1:\\c0prntsvr1v\REGFACHPLJ4350TN1", _
      "\\ricvc1\REGQAHPLJ4250TN:\\c0prntsvr1v\REGQAHPLJ4250TN", _
      "\\ricvc1\REGFACHPLJ4350TN:\\c0prntsvr1v\REGFACHPLJ4350TN", _
      "\\ricvc1\REGPACANIR3025:\\c0prntsvr1v\REGPACANIR3025", _
      "\\ricvc1\HP Color LaserJet CP2020 Series PCL 6:\\c0prntsvr1v\REGPHARMLEXX790", _
      "\\ricvc1\REGMAILKM552:\\c0prntsvr1v\REGMAILKM552", _
      "\\ricvc1\REGPAHPLJ4250TN2:\\c0prntsvr1v\REGPAHPLJ4250TN2", _
      "\\ricvc1\REGBUSDEVCANON3225:\\c0prntsvr1v\REGBUSDEVCANON3225", _
      "\\ricvc1\REGFACILITIESKM220:\\c0prntsvr1v\REGFACILITIESKM220", _
      "\\ricvc1\REGMKTHPLJ4250TN:\\c0prntsvr1v\REGMKTHPLJ4250TN", _
      "\\ricvc1\REGADMHPLJ4200TN:\\c0prntsvr1v\REGADMHPLJ4200TN", _
      "\\ricvc1\REGADMHPLJ8150TN:\\c0prntsvr1v\REGADMHPLJ3015", _
      "\\ricvc1\MISCANON:\\c0prntsvr1v\MISCANONLASER", _
      "\\ricvc1\REGMAILTOSHIBA6530:\\c0prntsvr1v\REGMAILTOSHIBA6530", _
      "\\ricvc1\REGHRCANIR1023iF:\\c0prntsvr1v\REGHRCANIR1023iF", _
      "\\ricvc1\REGPAWCHPLJ4250TN:\\c0prntsvr1v\REGPAWCHPLJ4250TN", _
      "\\ricvc1\REGPHYREL4015TN:\\c0prntsvr1v\REGPHYREL4015TN", _
      "\\ricvc1\REGPAHPLJ2430:\\c0prntsvr1v\REGPAHPLJ2430", _
      "\\ricvc1\REGMISOPSHPLJ3380:\\c0prntsvr1v\REGMISNTWKHPLJ3380", _
      "\\ricvc1\REGHRTRAINKM601:\\c0prntsvr1v\REGHRTRAINKM601", _
      "\\ricvc1\REGADMXERPHASER7700DN:\\c0prntsvr1v\Xerox Phaser 7760GX PS", _
      "\\ricvc1\REGOPERHPLJ4250TN:\\c0prntsvr1v\REGOPERHPLJ4250TN", _
      "\\ricvc1\REGMISHPLJ2430DTN:\\c0prntsvr1v\REGMISHPLJ2430DTN", _
      "\\ricvc1\REGPAHPLJ4250TN1:\\c0prntsvr1v\REGPAHPLJ4250TN1" _

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

strLogFile = strLogFileShare & objNetwork.UserName & ".txt"
Set objOutputFile = objFSO.OpenTextFile(strLogFile, intForAppending, True)
objOutputFile.WriteLine "Logged in at: " & Now & " on " & objNetwork.ComputerName
On Error Resume Next
If colPrinters.Count <> 0 Then
    For Each objPrinter In colPrinters
            For intPrinter = LBound(arrPrinterShares) To UBound(arrPrinterShares)
                  strOldPrinter = Split(arrPrinterShares(intPrinter), ":")(0)
                  strNewPrinter = Split(arrPrinterShares(intPrinter), ":")(1)
                  boolManual = False
                  If Left(strOldPrinter, 7) = "MANUAL=" Then
                        boolManual = True
                        strOldPrinter = Replace(strOldPrinter, "MANUAL=", "")
                  End If
                  If UCase(objPrinter.DeviceID) = UCase(strOldPrinter) Then
                        If boolManual = True Then
                              objOutputFile.WriteLine "MANUAL: Please obtain the account code for the printing 

from " & objPrinter.DeviceID & " and then replace that with " & strNewPrinter & " and set the account code."
                        ElseIf objPrinter.Default = True Then
                              objNetwork.RemovePrinterConnection objPrinter.DeviceID, True, True
                              objNetwork.AddWindowsPrinterConnection strNewPrinter
                              objNetwork.SetDefaultPrinter strNewPrinter
                              objOutputFile.WriteLine "Replaced " & objPrinter.DeviceID & " with " & 

strNewPrinter & " and set it as default"
                              objNetwork.RemovePrinterConnection objPrinter.DeviceID, True, True
                              objNetwork.AddWindowsPrinterConnection strNewPrinter
                              objOutputFile.WriteLine "Replaced " & objPrinter.DeviceID & " with " & 

                        End If                              
                  End If
            If Err.Number <> 0 Then
                  objOutputFile.WriteLine Err.Number & ": " & Err.Description & VbCrLf & objPrinter.DeviceID & " 

--> " & strNewPrinter
            End If
End If
On Error GoTo 0

Set objOutputFile = Nothing

MsgBox "Done"

Open in new window

Question by:pitster
    LVL 13

    Accepted Solution

    Try this, just change the old and new name in line 12 and 13
    Option Explicit
    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 = "\\sms-bus" 'This should be the name of the old server.
    to_sv = "\\sms-fp" 'This should be the name of your new server.
    'Just incase their are no printers and therefor no defauld printer set
    ' this will prevent the script form erroring out.
    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
    'You have to step by 2 because only the even numbers will be the print queue's
    ' server and share name. The odd numbers are the printer names.
    For Loop_Counter = 0 To WS_Printers.Count - 1 Step 2
    'Remember the + 1 is to get the full path ie.. \\your_server\your_printer.
    PrinterPath = lcase(WS_Printers(Loop_Counter + 1))
    'We only want to work with the network printers that are mapped to the original
    ' server, so we check for "\\Your_server".
    if LEFT(PrinterPath,len(from_sv)) = from_sv then
    'Now we need to parse the PrinterPath to get rhe Printer Name.
    spoint = instr(3,PrinterPath,"\")+1
    PrinterName = mid(PrinterPath,spoint,len(PrinterPath)-spoint+1)
    'Now remove the old printer connection.
    WshNet.RemovePrinterConnection from_sv+"\"+PrinterName
    'and then create the new connection.
    WshNet.AddWindowsPrinterConnection to_sv+"\"+PrinterName
    'If this printer matches the default printer that we got from the registry then
    ' set it to be the default printer.
    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

    LVL 3

    Author Comment


    Thanks for a quick response.

    This did not work on the Win 7 box at all, just like the other script.
    The only difference is your script does delete "\\ricvc1\HP Color LaserJet CP2020 Series PCL 6" on the Windows XP box, but it did not add the new mapping to "\\c0prntsvr1v\REGPHARMLEXX790" afterwards.
    LVL 13

    Expert Comment

    The share names need to be the same on the new and old print server then just run the script on all users
    LVL 3

    Author Comment

    Ok, after talking to one of my co-workers and a little leg-work, I found out all of the people that use that particular printer are already using the new server. So the script is perfect for XP.
    The only problem left is that it does not work on Windows 7 (I am using my Windows 7 Ent. 64bit machine as a test box). Any ideas on that?
    LVL 13

    Expert Comment

    is the print server 32 or 64 bit? Does the print server have the 32 and the 64 bit drivers installed on the printer share?
    LVL 3

    Author Comment

    The old print server is 32 bit. The new print server is 64 bit with both 32 and 64 bit drivers in the shares.
    LVL 13

    Expert Comment

    Ok so for giggles try running the script on a 32 bit machine, also manually install one of the test printers drivers (pointing to LPT1 or something)
    and then run the script and see if it moves the printer correctly. I am suspecting driver issue...
    I just ran this script from a 32 bit print server to a 64 and with the clients going from 32 to 64bit as well
    LVL 3

    Author Comment

    I am going to go ahead and accept your solution as the answer. After talking over the test scenarios, I was told to not even worry about Windows 7 because there are so few using it and a few are already pointed to the new server. I appreciate all the assistance you gave me, and although both scripts work now, your script runs faster than the old one I was using.

    Thanks again!
    LVL 3

    Author Closing Comment

    Great script for remapping users shared printers.

    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    Join & Write a Comment

    Printers have changed substantially in the last 30 or so years, not just in technical capabilities but in cost and usage as well.  Printers were originally used for interfacing with the operator, not necessarily for printing copy or pictures. In …
    Synchronize a new Active Directory domain with an existing Office 365 tenant
    This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles from a Windows Server 2008 domain controller to a Windows Server 2012 domain controlle…
    This tutorial will walk an individual through the process of configuring their Windows Server 2012 domain controller to synchronize its time with a trusted, external resource. Use Google, Bing, or other preferred search engine to locate trusted NTP …

    754 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

    25 Experts available now in Live!

    Get 1:1 Help Now