Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

login script to change printer server location

Status
Not open for further replies.

pwaterman

IS-IT--Management
Mar 7, 2007
3
0
0
US
we are planning on changing print servers, and was wondering if there was a script for login that could check the current printers installed and install to the new server. for example

current printer
\\hera\laserjet4050

change to
\\printserver\laserjet4050
 
Take a look at this thread thread931-1343298 and Marks FAQ which is linked from it as well.
 
I'm not sure where this came from originally, but we use it to migrate print servers seamlessly.
Just substiture the Oldserver and Newserver values for your environment and it should be good to go.

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("WScript.shell")

from_sv = "\\OLDSERVER" 'This should be the name of the old server.
to_sv = "\\NEWSERVER" 'This should be the name of your new server.

'Just incase their are no printers and therefore no default 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 = ""
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
'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 the 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
Next
Set WS_Printers = Nothing
Set WshNet = Nothing
Set WshShell = Nothing



 
Here's the one I always use. It has an unintended feature of usually keeping the same default printer.

Code:
' [URL unfurl="true"]http://www.tek-tips.com/faqs.cfm?fid=5798[/URL]

ON ERROR RESUME NEXT

Dim WSHShell, WSHNetwork, Path

Set WSHShell = CreateObject("WScript.Shell")
Set WSHNetwork = CreateObject("WScript.Network")
Set WSHPrinters = WSHNetwork.EnumPrinterConnections
strOldPrintSrvrPath = "\\oldServer\"
strNewPrintSrvrPath = "\\newServer\"
Function swapPrinter (oldPrinter,newPrinter)
For LOOP_COUNTER = 0 To WSHPrinters.Count - 1 Step 2
    If LCase(WSHPrinters.Item(LOOP_COUNTER +1)) = LCase(strOldPrintSrvrPath & oldPrinter) Then
      ' To do: find out if it's the default printer
      WSHNetwork.RemovePrinterConnection (strOldPrintSrvrPath & oldPrinter),True,True 
    	WSHNetwork.AddWindowsPrinterConnection (strNewPrintSrvrPath & newPrinter)  
    	' To do: if oldPrinter was the default printer, make newPrinter the default
    	' WSHNetwork.SetDefaultPrinter (strNewPrintSrvrPath & newPrinter)
    End If
Next
End Function

' syntax for next line is "old server name", "new server name"
' this is NOT the share name!
swapPrinter "HP LaserJet 4050 North","HP LaserJet 4050 South"

'Clean Up Memory We Used
set WSHNetwork = Nothing
set WSHSHell = Nothing
Set WSHPrinters = Nothing
  
'Quit the Script
wscript.quit

Pat Richard, MCSE MCSA:Messaging CNA
Microsoft Exchange MVP
Want to know how email works? Read for yourself -
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top