Jump to content

Print Server Migration Script


Recommended Posts

Guest bolzy1981@gmail.com
Posted

Hello,

 

I'd like to modify this script I found on the web so that it will not

remove any printers attached to the print servers other than the one

specified in the script, this is an example of what I want it to do

 

Scenario:

3 Print Servers

- Print1

- Print1A

- Print2

 

Client PC has 3 printers installed

- P001 on server Print1

- P002 on server Print1

- P010 on server Print2

 

Action:

How would I modify the below script so that it only removes the

printers from server Print1, and readds them from server Print1A -

leaving any printers that are attached to server Print2

 

 

=========================================

On Error Resume Next

 

'Creates variables for querying the default printer

Function GetDefaultPrinter()

sRegVal = "HKCU\Software\Microsoft\Windows NT\CurrentVersion\Windows

\Device"

sDefault = ""

 

'Queries the registry for the default printer

On Error Resume Next

sDefault = objShell.RegRead(sRegVal)

sDefault = Left(sDefault ,InStr(sDefault, ",") - 1)

On Error Goto 0

GetDefaultPrinter = sDefault

End Function

 

Set objNetwork = CreateObject ("Wscript.Network")

Set objShell = CreateObject ("WScript.Shell")

 

strComputer = "."

PrintServer = "Print1A"

PrintServer = LCase (PrintServer)

 

Set objWMIService = GetObject("winmgmts:" & _

"{impersonationLevel=Impersonate}!\\" & strComputer & "\root\cimv2")

 

If Err.Number Then

wscript.echo ("Error : " & Err.Number & ": " & Err.Description &

VbCrLf)

Err.Clear

 

Else

ImpDefault = GetDefaultPrinter

Set colInstalledPrinters = objWMIService.ExecQuery _

("SELECT * FROM Win32_Printer")

For Each objPrinter in colInstalledPrinters

PrinterArray = Split (objPrinter.Name , "\")

 

If (LCase(objPrinter.ServerName) <> "") and

(LCase(objPrinter.ServerName) <> "\\" & PrintServer) then

 

objNetwork.AddWindowsPrinterConnection "\\" & PrintServer & "\" &

PrinterArray(3)

If Err.Number Then

wscript.echo ("Error : " & Err.Number & ": " & Err.Description &

VbCrLf)

Err.Clear

End If

 

If ImpDefault = objPrinter.Name then

objNetwork.SetDefaultPrinter ("\\" & PrintServer & "\" &

PrinterArray(3))

End If

 

objNetwork.RemovePrinterConnection objPrinter.Name

End If

 

 

Next

End If

=========================================

 

 

 

 

 

 

Thanks,

Marcus

  • Replies 2
  • Created
  • Last Reply
Guest Masterplan
Posted

RE: Print Server Migration Script

 

Hi,

 

You can do this by a much simpler script like this:

On Error Resume Next

strComputer = "."

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set wn = WScript.CreateObject("WScript.Network")

Set ntsm = WScript.CreateObject("WScript.Shell")

Set colItems = objWMIService.ExecQuery("Select * from Win32_Printer",,48)

For Each objItem in colItems

Dim printer, server

printer = objItem.ShareName

server = objItem.ServerName

If server Is "\\Print1" Then

wn.RemovePrinterConnection "\\Print1\" &printer

wn.AddWindowsPrinterConnection "\\Print1A\" &printer, printer, True

End If

Next

 

The only thing you should care about is that printers must have the same

share name on both servers.

 

Have a nice day!

 

 

"bolzy1981@gmail.com" wrote:

> Hello,

>

> I'd like to modify this script I found on the web so that it will not

> remove any printers attached to the print servers other than the one

> specified in the script, this is an example of what I want it to do

>

> Scenario:

> 3 Print Servers

> - Print1

> - Print1A

> - Print2

>

> Client PC has 3 printers installed

> - P001 on server Print1

> - P002 on server Print1

> - P010 on server Print2

>

> Action:

> How would I modify the below script so that it only removes the

> printers from server Print1, and readds them from server Print1A -

> leaving any printers that are attached to server Print2

>

>

> =========================================

> On Error Resume Next

>

> 'Creates variables for querying the default printer

> Function GetDefaultPrinter()

> sRegVal = "HKCU\Software\Microsoft\Windows NT\CurrentVersion\Windows

> \Device"

> sDefault = ""

>

> 'Queries the registry for the default printer

> On Error Resume Next

> sDefault = objShell.RegRead(sRegVal)

> sDefault = Left(sDefault ,InStr(sDefault, ",") - 1)

> On Error Goto 0

> GetDefaultPrinter = sDefault

> End Function

>

> Set objNetwork = CreateObject ("Wscript.Network")

> Set objShell = CreateObject ("WScript.Shell")

>

> strComputer = "."

> PrintServer = "Print1A"

> PrintServer = LCase (PrintServer)

>

> Set objWMIService = GetObject("winmgmts:" & _

> "{impersonationLevel=Impersonate}!\\" & strComputer & "\root\cimv2")

>

> If Err.Number Then

> wscript.echo ("Error : " & Err.Number & ": " & Err.Description &

> VbCrLf)

> Err.Clear

>

> Else

> ImpDefault = GetDefaultPrinter

> Set colInstalledPrinters = objWMIService.ExecQuery _

> ("SELECT * FROM Win32_Printer")

> For Each objPrinter in colInstalledPrinters

> PrinterArray = Split (objPrinter.Name , "\")

>

> If (LCase(objPrinter.ServerName) <> "") and

> (LCase(objPrinter.ServerName) <> "\\" & PrintServer) then

>

> objNetwork.AddWindowsPrinterConnection "\\" & PrintServer & "\" &

> PrinterArray(3)

> If Err.Number Then

> wscript.echo ("Error : " & Err.Number & ": " & Err.Description &

> VbCrLf)

> Err.Clear

> End If

>

> If ImpDefault = objPrinter.Name then

> objNetwork.SetDefaultPrinter ("\\" & PrintServer & "\" &

> PrinterArray(3))

> End If

>

> objNetwork.RemovePrinterConnection objPrinter.Name

> End If

>

>

> Next

> End If

> =========================================

>

>

>

>

>

>

> Thanks,

> Marcus

>

Guest bolzy1981@gmail.com
Posted

Re: Print Server Migration Script

 

That's fantastic! Thank you so much!


×
×
  • Create New...