正在过滤WMI查询

问题描述 投票:0回答:1

我有一些VBS,用于查询域计算机以获取其已安装的打印机,并在四列中将详细信息输出到电子表格。

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"

这很容易并且有效,但是如果我尝试过滤或缩小返回的值以删除软件打印机如Adobe PDF等,我将从输出中丢失打印机的“名称”信息。当此方法有效时,我将获得每台打印机的名称,类型,PC名称和分配的用户(来自脚本另一部分的变量):

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

但是如果我更改Set colPrinters = xxx行以使用任何方法进行过滤,例如:

Set colPrinters = objWMIService.ExecQuery("Select * From Win32_Printer" & "Where Name = 'HP'%'")

或尝试使用比较运算符,通过变量objPrinter.Name输出的信息不会出现在电子表格中。我尝试了4种不同的方法来执行此操作,但结果是相同的,如果更改该行,我会丢失打印机名称。

vbscript wmi-query
1个回答
0
投票

是否可以像您在“ Win32_Printer”和“ Where”之间不加空格的事实那样简单? “ Win32_PrinterWhere”将不起作用。

© www.soinside.com 2019 - 2024. All rights reserved.