Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Possible to access a WMI object's properties BY NAME in VBScript?

Tags:

com

vbscript

wmi

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
like image 536
tbone Avatar asked Nov 23 '10 03:11

tbone


1 Answers

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
like image 53
Helen Avatar answered Jan 03 '23 01:01

Helen