【发布时间】:2018-03-06 13:46:50
【问题描述】:
我有一点 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 种不同的方法来做到这一点,但结果是相同的,如果更改该行,我会丢失我的打印机名称。
【问题讨论】:
-
您似乎正在尝试将 PowerShell 样式的过滤器与 WQL 查询混合使用。试试
"Select * from Win32_Printer WHERE Name LIKE 'HP%'" -
直接从 MSDN 中提取它,但是是的,没有正确读取有关它来自 PS 的信息。但是,我已经尝试过 "Where Name LIKE" "Where Name 'Adobe*'" 等等。我尝试过使用各种类型的比较器、逻辑运算符等。它确实成功地改变了输出的内容,但它也删除了打印机名称,这主要是我在输出中寻找的内容。我只是不明白为什么添加过滤器会更改 colPrinter 中保存的数据,除非它在代码中更进一步。