I have a bit of VBS that is used to query domain machines for their installed printers and output details to a spreadsheet in four columns.
objExcel.Cells(1,1).Value = "Machine Name"
objExcel.Cells(1,2).Value = "Printer Name"
objExcel.Cells(1,3).Value = "Type"
objExcel.Cells(1,4).value = "Description"
That's easy enough and it works, but if I try to filter or narrow the returned values to remove the software printers like Adobe PDF etc, I lose the printer "Name" information from the output. When this works I get every printer, it's name and type, along with PC name and assigned user (from variables in another part of the script):
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
If Err.Number = 0 Then
Set colPrinters = objWMIService.ExecQuery("Select * From Win32_Printer")
For Each objPrinter In colPrinters
If objPrinter.Attributes And 64 Then
strPrinterType = "Local"
Else
strPrinterType = "Network"
End If
objExcel.Cells(intRow, 1).Value = strComputer
objExcel.Cells(intRow, 2).Value = objPrinter.Name
objExcel.Cells(intRow, 3).Value = strPrinterType
objExcel.Cells(intRow, 4).Value = strOwner
intRow = intRow + 1
Next
But if I change the line Set colPrinters = xxx
to filter using any method e.g.:
Set colPrinters = objWMIService.ExecQuery("Select * From Win32_Printer" & "Where Name = 'HP'%'")
or try to use comparison operators, the information that would be output through the variable objPrinter.Name
doesn't appear in the spreadsheet.
I've tried 4 different ways to to do this but the results are the same, if change that line I lose my printer names.