none
Printer VBS scripting migration RRS feed

  • Question

  • Hi all,

      I am new to this forum so please excuse me if I miss some common rules on this forum. I am trying to migrate all my organization printers from a old server to a new server. However, I am unable to run this script to transfer the printer to the new server. All drivers are located on the new server, but i am having issue with running the VBS script. Please help.

    Also, I read online that i need to post this script in group policy in order for this work. Can someone confirm on this.

    strOldServer = "CABERNET"
    strNewServer = "PrintServer1"
    strComputer = "."

    Set WSHNetwork = CreateObject("WScript.Network")
    Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
    Set colInstalledPrinters =  objWMIService.ExecQuery("Select * from Win32_Printer")

    ' Get the name of the local computer
    set objWshShell = CreateObject("WScript.Shell")
    set colNames = objWMIService.ExecQuery("Select * from Win32_ComputerSystem")
    strLocalComputerName = ""
    for each objName in colNames
     strLocalComputerName = objName.Name
    next

    ' Setup variables for results file
    Const ForAppending = 8
    Set objFSO = CreateObject("Scripting.FileSystemObject")

    ' Set filepath and name where results will be written
    ' Example filename: MERLOT_PrinerMigrationResults.log
    resultsFilePath = "c:\" & strLocalComputerName & "_PrinterMigrationResults.log"

    ' Check if results file exists on local system. If it does, terminate script.
    if objFSO.FileExists(resultsFilePath) then
    WScript.Quit
    end if

    ' Create log file on local system
    Set objTextFile = objFSO.OpenTextFile(resultsFilePath, ForAppending, True)

    ' Write the name of the computer to the results file
    objTextFile.WriteLine(strLocalComputerName & vbCrLf)


    strOldServer = prepServer(strOldServer)
    strNewServer = prepServer(strNewServer)

    For Each objPrinter in colInstalledPrinters
       strName = objPrinter.Name
       iPrinterLocation = InStr(UCase(objPrinter.Name),UCase(strOldServer))

       ' The printer needs to be updated
       If iPrinterLocation > 0 then
          strPrinter = strNewServer & Right(strName, Len(strName) - Len(strOldServer))
          objPrinter.Delete_

          ' Statement used to catch error.  Non-fatal errors should no longer
          ' cause the script to terminate.  Script execution will resume at the
          ' next statement.
          on error resume next

          ' Try to install the updated printer
          WSHNetwork.AddWindowsPrinterConnection strPrinter

          ' An error occured while installing the printer. Handle it.
          if Err.Number <> 0 then
             ' Write error information to the results file
             objTextFile.WriteLine("ERROR : " & Err.Description & " (" & Err.Number & ")")
             ' Clear the error before trying to add another printer
             Err.Clear

          ' Printer was successfully installed
          else 
             ' Write the name of the updated printer to the results file
             objTextFile.WriteLine(strPrinter & vbCrLf)         
          end if

          If objPrinter.Default = True Then
             WSHNetwork.SetDefaultPrinter strPrinter
          End If
       End If
    Next

    ' Close the results file before proceeding
    objTextFile.Close

    ' Set the path on FS1 where the log should be copied to
    remoteFilePath = "\\fs1\it\"

    ' Copy the results file to the remote server
    objFSO.CopyFile resultsFilePath, remoteFilePath 


    Function prepServer(strServer)
        If Left(strServer, 2) <> "\\" then
          strServer = "\\" & strServer
       End If
       If Right(strServer, 1) <> "\" then
          strServer = strServer & "\"
       End If
       prepServer = strServer
    End Function

                          
    Friday, August 31, 2012 10:15 PM

Answers

  •  

    Windows server 2008r2 Print Manager will do this for you in two easy steps.


    ¯\_(ツ)_/¯

    • Marked as answer by GammaBetaZero Monday, September 24, 2012 4:55 PM
    Friday, August 31, 2012 10:35 PM

All replies

  •  

    Windows server 2008r2 Print Manager will do this for you in two easy steps.


    ¯\_(ツ)_/¯

    • Marked as answer by GammaBetaZero Monday, September 24, 2012 4:55 PM
    Friday, August 31, 2012 10:35 PM
  • We are currently running window server 2003. 
    Tuesday, September 4, 2012 8:44 PM
  • We are currently running window server 2003. 

    You are migrating from WS2003 to Ws2003?

    You say you are having a problem but you do not say what the problem is.

    WS2003R2 also has PrintManger.


    ¯\_(ツ)_/¯

    Tuesday, September 4, 2012 9:16 PM