Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.
January 29, 2006
A. The following code, which you can download here , searches for a connected network printer and replaces it with an alternate:
Option ExplicitDim objWMIService, objPrinter, colItems, strComputer, objWshShell, strDefaultStatestrComputer ="."Set objWshShell = WScript.CreateObject("WScript.Shell")Set objWMIService = GetObject("winmgmts:\" & strComputer & "rootCIMV2")' For Remote:' Set Locator = CreateObject("WbemScripting.SWbemLocator") ' Set objWMIService = Locator.ConnectServer(strComputer, "rootcimv2", strUserName, strPassword) Set colItems = objWMIService.ExecQuery ("SELECT * FROM Win32_Printer")' On Error Resume NextFor Each objPrinter In colItemsIf UCase(objPrinter.DeviceID) = UCase("\savdalprinterOldprinter1") Then Wscript.Echo "Found match " & objPrinter.DeviceID & ", replacing" ' Add new printer. Need the ,1,true to wait for shell to complete before continue objWshShell.Run "rundll32 printui.dll,PrintUIEntry /in /Gw /q /n \savdaldc01printerNew",1, true ' Remove the old objWshShell.Run "rundll32 printui.dll,PrintUIEntry /dn /q /n \savdalprinterOldprinter1",1, true If objPrinter.Default Then ' if it's the default set as default objWshShell.Run "rundll32 printui.dll,PrintUIEntry /y /n \savdaldc01printerNew",1, true End IfEnd If NextWScript.Quit
You can change the printer the code searches for and what it maps to when it finds a match.
You May Also Like