可以在VBScript中访问WMI对象的属性BY NAME吗?

时间:2010-11-23 03:12:04

标签: com vbscript wmi

而不是像这样的代码:

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

1 个答案:

答案 0 :(得分:8)

您可以通过Properties_属性访问WMI对象的命名属性:

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