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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With