【问题标题】:VBS Invalid Query in Windows 10Windows 10 中的 VBS 无效查询
【发布时间】:2017-07-07 05:25:12
【问题描述】:

为什么以下 VBS 代码在 Windows 7 中可以工作,但在 Windows 10 上却报错:

strComputer="."
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colProcesses = objWMIService.ExecQuery("Select * from Win32_Process Where Name = 'notepad.exe'")
Wscript.Echo colProcesses.Count

我在 Windows 10 中遇到的错误是:

test_2.vbs(4,1) SWbemObjectSet:无效查询

我在这里做错了什么?

【问题讨论】:

  • 您的代码在我的系统上运行良好(不过使用 Win 7)。
  • 是的,正如我在我的问题中提到的,它适用于 Windows 7,但不适用于 Windows 10
  • 哦..我没注意到:P

标签: vbscript wmi


【解决方案1】:

尝试使用以下代码。这应该工作

strQuery = "select * from win32_process where Name = " & """" & "Notepad.exe" & """"
strComputer="."
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colProcesses = objWMIService.ExecQuery(strQuery)
Wscript.Echo colProcesses.Count

【讨论】:

  • 是的,完美。由于我仍在学习 VBS,您能否解释一下为什么 strQuery 中有很多 " 标记。
  • 了解 Notepad.exe 在 Windows 10 中需要双引号,例如 "Notepad.exe",但可以使用单引号 'Notepad.exe'。这很奇怪。如果有人有这个原因,请解释。谢谢。
  • 不需要额外的连接,只需记住将字符串变量中的文字引号加倍,如下所示:strQuery = "select * from win32_process where Name = ""Notepad.exe"""
  • 解释(在答案中,而不是在 cmets 中)将大大提高答案的价值。
猜你喜欢
  • 1970-01-01
  • 2015-11-16
  • 1970-01-01
  • 1970-01-01
  • 2012-08-05
  • 1970-01-01
  • 1970-01-01
  • 2018-05-31
  • 1970-01-01
相关资源
最近更新 更多