• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 271
  • Last Modified:

Migration of printe server through vbs

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"
   Else
       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 & "\" & _
           ShareName(PrinterPath)
           If Err.Number = 0 Then
               Set objFile = wscript.CreateObject("Scripting.FileSystemObject")
               If Not objFile.FolderExists("c:\printers_remapped") Then
               objFile.CreateFolder "c:\printers_remapped"
               objFile.Close
               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
Next

'Set the default printer:
If ServerName(DefaultPrinter) = OldServer Then
   On Error Resume Next
   WshNetwork.SetDefaultPrinter "\\" & NewServer & "\" & _
   ShareName(DefaultPrinter)
   '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
'--------------------8<----------------------
any help would be welcome
erwin miranda
Move-Printers-to-New-Server.txt
0
erwin_miranda
Asked:
erwin_miranda
1 Solution
 
danleinCommented:
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("WScript.shell")
 
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 = ""
else
 
'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
Next
 
Set WS_Printers = Nothing
Set WshNet = Nothing
Set WshShell = Nothing

Open in new window

0
 
erwin_mirandaAuthor Commented:
Thanks for the Help man..
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now