VBScript:如何检查SWbemObjectSet的有效性?

发布于 2024-09-03 07:52:01 字数 790 浏览 3 评论 0原文

我有以下 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

坦然微笑 2024-09-10 07:52:01

编辑;

.Count 似乎适用于模式查询;

dim testNs: testNs = "Win32_DoesNotExist"
dim colClasses: set colClasses = Wmi.ExecQuery("Select * From Meta_Class where __Class = """ & testNs  & """")
msgbox colClasses.count

您可以包装并捕获访问错误;

SET QR = Wmi.ExecQuery("SELECT * FROM Win32_DoesNotExist")

dim i: i = getCount(QR)

if (i < 0) then
    msgbox "oopsy"
else
    msgbox "count is " & i
end if

function getCount(wmiCol)
    on error resume next
    getCount = QR.Count
    if (err.number <> 0) then getCount = (-1)
    on error goto 0
end function

Edit;

.Count seems to work for a schema query;

dim testNs: testNs = "Win32_DoesNotExist"
dim colClasses: set colClasses = Wmi.ExecQuery("Select * From Meta_Class where __Class = """ & testNs  & """")
msgbox colClasses.count

You could wrap-n-trap the access error;

SET QR = Wmi.ExecQuery("SELECT * FROM Win32_DoesNotExist")

dim i: i = getCount(QR)

if (i < 0) then
    msgbox "oopsy"
else
    msgbox "count is " & i
end if

function getCount(wmiCol)
    on error resume next
    getCount = QR.Count
    if (err.number <> 0) then getCount = (-1)
    on error goto 0
end function
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文