可以在 VBScript 中按名称访问 WMI 对象的属性吗?

发布于 2024-10-04 01:49:14 字数 967 浏览 5 评论 0原文

而不是这样的代码:

On Error Resume Next
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_IP4RouteTable",,48)
For Each objItem in colItems
    Wscript.Echo "Age: " & objItem.Age
    Wscript.Echo "Caption: " & objItem.Caption
    Wscript.Echo "Description: " & objItem.Description
Next

是否可以通过名称访问每个属性,例如以下语法之一:

Wscript.Echo "Age: " & objItem("Age")
Wscript.Echo "Age: " & objItem.Properties("Age")
Wscript.Echo "Age: " & objItem.Item("Age")

更好的是,有什么方法可以执行以下操作:

Dim colItems
Dim objItem
Dim aProperty
Set colItems = objWMIService.ExecQuery("Select * from Win32_IP4RouteTable",,48)
For Each objItem in colItems
   For Each aProperty in objItem.Properties
       Wscript.Echo aProperty.Name & ": " & objItem(aProperty.Name)
   Next
Next

Instead of code like this:

On Error Resume Next
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_IP4RouteTable",,48)
For Each objItem in colItems
    Wscript.Echo "Age: " & objItem.Age
    Wscript.Echo "Caption: " & objItem.Caption
    Wscript.Echo "Description: " & objItem.Description
Next

Is it possible to access each property by name, something like one of these syntaxes:

Wscript.Echo "Age: " & objItem("Age")
Wscript.Echo "Age: " & objItem.Properties("Age")
Wscript.Echo "Age: " & objItem.Item("Age")

And even better, is there any way you can do something like:

Dim colItems
Dim objItem
Dim aProperty
Set colItems = objWMIService.ExecQuery("Select * from Win32_IP4RouteTable",,48)
For Each objItem in colItems
   For Each aProperty in objItem.Properties
       Wscript.Echo aProperty.Name & ": " & objItem(aProperty.Name)
   Next
Next

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

橙幽之幻 2024-10-11 01:49:14

您可以通过 Properties_< 访问 WMI 对象的命名属性/a> property:

objItem.Properties_("Age")
objItem.Properties_.Item("Age")

当然,你也可以枚举 Properties_ 集合:

For Each objItem in colItems
  For Each prop in objItem.Properties_
    If IsArray(prop) Then
      WScript.Echo prop.Name & ": " & Join(prop, ", ")
    Else
      Wscript.Echo prop.Name & ": " & prop
      ''# -- or --
      ''# Wscript.Echo prop.Name & ": " & prop.Value
    End If
  Next
Next

You can access named properties of WMI objects via the Properties_ property:

objItem.Properties_("Age")
objItem.Properties_.Item("Age")

And of course, you can also enumerate the Properties_ collection:

For Each objItem in colItems
  For Each prop in objItem.Properties_
    If IsArray(prop) Then
      WScript.Echo prop.Name & ": " & Join(prop, ", ")
    Else
      Wscript.Echo prop.Name & ": " & prop
      ''# -- or --
      ''# Wscript.Echo prop.Name & ": " & prop.Value
    End If
  Next
Next
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文