Can I access the properties of a WMI BY NAME object in VBScript?

Instead of this code:

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 a way to 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
+5
source share
1 answer

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

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

And of course, you can also list the collection 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
+7
source

All Articles