VBScript:如何检查SWbemObjectSet的有效性?
我有以下 VBScript:
SET Wmi = GetObject("winmgmts:\\.\root\cimv2")
SET QR = Wmi.ExecQuery("SELECT * FROM Win32_Processor")
MsgBox("" & QR.Count)
工作得很好。但是,当我查询不存在的内容时:
SET Wmi = GetObject("winmgmts:\\.\root\cimv2")
SET QR = Wmi.ExecQuery("SELECT * FROM Win32_DoesNotExist")
MsgBox("" & QR.Count)
我收到以下错误消息:
Script: E:\test.vbs
Line: 3
Char: 1
Error: Invalid class
Code: 80041010
Source: SWbemObjectSet
如何知道 QR
对象是否有效?
如果我调用 TypeName(QR)
,它会显示 SWbemObjectSet
,但是一旦我尝试查询其属性之一,就会失败并显示上述消息。
我用谷歌搜索了这个错误,大多数页面似乎都说了一些“只是不要执行该查询”的内容。不幸的是,这不是一个选项,因为我想在多个版本的 Windows 上运行相同的脚本,并且 Microsoft 有时会在新版本的 Windows 中弃用 WMI 类。我希望我的脚本能够优雅地处理这个问题。
I have the following VBScript:
SET Wmi = GetObject("winmgmts:\\.\root\cimv2")
SET QR = Wmi.ExecQuery("SELECT * FROM Win32_Processor")
MsgBox("" & QR.Count)
Which works perfectly fine. However, when I query something which doesn't exist:
SET Wmi = GetObject("winmgmts:\\.\root\cimv2")
SET QR = Wmi.ExecQuery("SELECT * FROM Win32_DoesNotExist")
MsgBox("" & QR.Count)
I get the following error message:
Script: E:\test.vbs
Line: 3
Char: 1
Error: Invalid class
Code: 80041010
Source: SWbemObjectSet
How can I know whether the QR
object is valid?
If I call TypeName(QR)
, it will say SWbemObjectSet
, but as soon as I try to query one of its properties, it fails with the above message.
I've googled for this error, and most pages seem to say something to the effect of "just don't do that query". This is not an option, unfortunately, because I want to run the same script on multiple versions of Windows, and Microsoft occasionally deprecates WMI classes in new versions of Windows. I want my script to handle that gracefully.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
编辑;
.Count
似乎适用于模式查询;您可以包装并捕获访问错误;
Edit;
.Count
seems to work for a schema query;You could wrap-n-trap the access error;