'************************************************************************************************* ' Script Name: PrinterReconnect.vbs ' Author: Steve Ross, Catapult Systems, Inc. ' Purpose: After migrating print queues to a new server this script can be used to disconnect ' the old queues and connect to the new queues. It also will set the default ' printer to the same device it was set to previously. The script works based on an ' input file. The file must be customized based on your printer migration. Place ' the old queue definition (url to the print share) and the new queue definition on ' the same line separated by a "~". '************************************************************************************************* On Error Resume Next Const ForReading = 1 Set WshNetwork = CreateObject("WScript.Network") Set fso = CreateObject("Scripting.FileSystemObject") If Not fso.FileExists("c:\PrinterReconnectFinished.txt") then Set f1 = fso.OpenTextFile(GetPath() & "input.txt", ForReading) varDefault = GetDefaultPrinter() tmpArr = Split(f1.readall, vbcrlf) for i = 0 to UBOUND(tmpArr) If instr(1, tmpArr(i), varDefault, 1) then x = i err.clear tmpVar = split(tmpArr(i), "~") WshNetwork.RemovePrinterConnection tmpVar(0) If err.number = 0 then WshNetwork.AddWindowsPrinterConnection tmpVar(1) next var1 = split(tmpArr(x), "~") ConfigDefaultPrinter(var1(1)) Set f2 = fso.CreateTextFile("c:\PrinterReconnectFinished.txt", True) Set f3 = fso.Getfile("c:\PrinterReconnectFinished.txt") f3.attributes = f3.attributes + 2 End If '************************************************************************************************* Sub ConfigDefaultPrinter(name) '***************************************************************************************** '***************************************************************************************** ' Purpose: This sub routein defines a default printer based on the printer name passed to ' it. ' Version 1.0 ' Author Steve Ross, Catapult Systems, Inc. ' Arguements: None '***************************************************************************************** '***************************************************************************************** strComputer = "." Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & _ strComputer & "\root\cimv2") Set colInstalledPrinters = objWMIService.ExecQuery _ ("Select * from Win32_Printer Where Name = 'name'") For Each objPrinter in colInstalledPrinters objPrinter.SetDefaultPrinter() Next End Sub '************************************************************************************************* '************************************************************************************************* Function GetDefaultPrinter() '***************************************************************************************** '***************************************************************************************** ' Purpose: This Function returns the default printer as configured on the client. ' Version 1.0 ' Author Steve Ross, Catapult Systems, Inc. ' Arguements: None ' Returns: Text string '***************************************************************************************** '***************************************************************************************** strComputer = "." Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & _ strComputer & "\root\cimv2") Set colInstalledPrinters = objWMIService.ExecQuery("Select * from Win32_Printer") For Each objPrinter in colInstalledPrinters If objPrinter.Default then GetDefaultPrinter = objPrinter.Name End If Next End Function '************************************************************************************************* '************************************************************************************************* Function GetPath() '********************************************************************************************* '********************************************************************************************* ' Purpose: This Function returns the path where the script is currently being executed. ' Version 1.0 ' Author Bryan Taylor ' Arguements: None ' Returns: Text string '********************************************************************************************* '********************************************************************************************* Dim path path = WScript.ScriptFullName GetPath = Left(path, InStrRev(path, "\")) End Function '*************************************************************************************************