-1

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.

Martin
  • 21
  • 1
  • 1
  • 5
  • 3
    It looks like you're trying to mix PowerShell-style filters with WQL queries. Try `"Select * from Win32_Printer WHERE Name LIKE 'HP%'"` – Jeff Zeitlin Mar 06 '18 at 13:51
  • [Related](https://msdn.microsoft.com/en-us/library/aa392902.aspx). – Ansgar Wiechers Mar 06 '18 at 14:59
  • Pulled it straight from MSDN but yes, did not correctly read the bit about it being from PS. BUT, I have tried "Where Name LIKE" "Where Name <> 'Adobe*'" and so-on. I have tried using various types of comparator, logical operators etc. It does succesfully change the content of the output, but it also removes the printer name, which is primarily what I'm looking for in the output. I just don't understand why adding a filter changes the data held in colPrinter, unless it's further down in the code. – Martin Mar 07 '18 at 08:48

1 Answers1

0

Could it be as simple as the fact that you didn't put a space between "Win32_Printer" and "Where"? "Win32_PrinterWhere" won't work.

Gordon
  • 1