I have an environment with several hundred computers. I am trying to write vBscript that runs and adds the appropriate printer. The printer names can be determined using clues about the computer name. My script (a little dirty due to debugging) is as follows:
'Key vars
printServer = "SERVER"
Set WshNetwork = CreateObject("WScript.Network")
'Extract computer name and take the first two fields
cNameParts=Split(WshNetwork.ComputerName,"-")
printerNamePrefix = cNameParts(0) + "-P" + cNameParts(1)
Set objWMIService = GetObject("winmgmts:\\" & printServer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select Name from Win32_Printer WHERE Name LIKE '" + printerNamePrefix + "-%'",,48)
'Set colItems = objWMIService.ExecQuery("Select Name from Win32_Printer",,48)
Wscript.Echo "Done querying.."
printerName = ""
For Each printerObj in colItems
printerName = printerObj.Name
Wscript.Echo printerName
Next
PrinterPath = "\\" + UCASE(printServer) + "\" + printerName
Wscript.Echo "Adding " + PrinterPath
WshNetwork.AddWindowsPrinterConnection PrinterPath, PrinterDriver
WshNetwork.SetDefaultPrinter PrinterPath
Now WMI seems to be painfully slow and pegs the CPU on the server. When I comment the first query and uncomment the second (to enumerate all printers) it seems to buffer by outputting:
PRINTER_NAME
PRINTER_NAME
PRINTER_NAME
PRINTER_NAME
PRINTER_NAME
PRINTER_NAME
PRINTER_NAME
(wait a few sec)
PRINTER_NAME
PRINTER_NAME
PRINTER_NAME
PRINTER_NAME
PRINTER_NAME
PRINTER_NAME
PRINTER_NAME
(wait a few sec)
I know WMI returns async, that's why "done querying" prints first. Now apparently "net view" doesn't use WMI since it is super fast. I could write a VBScript to parse "net view" but that seems very clunky. Is there a better way?