0

我正在尝试在数组中搜索打印机,如果打印机存在,则在 HTA 中显示名称。该位工作正常,但是当在阵列中找不到打印机时,将显示设备上所有已安装的打印机。有没有办法只显示找到的打印机

Set objFSO = CreateObject("Scripting.FileSystemObject")
arrPrinters = Split(objFSO.OpenTextFile("C:\Windows\DEW\denied-printers.txt" ,ForReading).ReadAll(), VbCrLf)

Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colInstalledPrinters =  objWMIService.ExecQuery("Select * from Win32_Printer")
For Each objPrinter in colInstalledPrinters
    localprinter = objPrinter.Name
    For Each strLine in arrPrinters
        If inStr(localprinter,strLine) > 0 Then 
            strHTML = strHTML & "<tr><td>" & localprinter & "</td></tr>"
    End If 
Next
Next
4

2 回答 2

1

试试这个

If inStr(localprinter,strLine) > 0 OR inStr(localprinter,strLine) = NULL Then 

End If

问题是如果数组为空,strline 为 NULL,当你在 inStr 中使用它时,它返回 NULL 而不是 '0'。这是一种可能性 – tunmise fasipe 3 分钟前 编辑

于 2012-05-30T20:42:50.470 回答
0

你的主要问题 - 我猜不知道 yoyr 文件的内容 - 是 instr(textToSearch, searchString) 你切换了两个参数。无论如何,这是我测试过的代码版本。

const ForReading = 1
strComputer = "."
set objFSO = createObject("Scripting.FileSystemObject")
printers = objFSO.OpenTextFile("denied-printers.txt" ,ForReading).ReadAll()
set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
set colInstalledPrinters =  objWMIService.ExecQuery("Select * from Win32_Printer")
for Each objPrinter in colInstalledPrinters
   localprinter = objPrinter.Name
   if instr(printers, localprinter) then
     strHTML = strHTML & "<tr><td>" & localprinter & "</td></tr>"
   end if
next

编辑:这里是独立的 vbscript 版本,将其保存到 .vbs 文件并运行以进行测试

on error resume next
const ForReading = 1 
strComputer = "." 
file = "denied-printers.txt"
set objFSO = createObject("Scripting.FileSystemObject")
set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") 
set colInstalledPrinters =  objWMIService.ExecQuery("Select * from Win32_Printer")
if err.number=0 then
  printers = objFSO.OpenTextFile(file ,ForReading).ReadAll()
  if err.number=0 then
    set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") 
    set colInstalledPrinters =  objWMIService.ExecQuery("Select * from Win32_Printer") 
    for Each objPrinter in colInstalledPrinters 
       localprinter = objPrinter.Name 
       if instr(printers, localprinter) then 
         wscript.echo localprinter & " found in " & file
       end if 
    next 
  else
    wscript.echo "file " & file & " not found, showing all printers"
    for Each objPrinter in colInstalledPrinters 
      wscript.echo objPrinter.Name
    next 
  end if
else
  wscript.echo "Error" & err.description
end if
于 2012-05-30T22:10:31.550 回答