Jump to content

Using variables with objects


Recommended Posts

Hello,

Im using various WMI queries to extract information on a PC. I'd have a bit of code like this...

$colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_BaseBoard", "WQL", $wbemFlagReturnImmediately + $wbemFlagForwardOnly)
If IsObj($colItems) then
    Dim $data_motherboard[5]
    For $objItem In $colItems
        $data_motherboard[0] = $objItem.Manufacturer
        $data_motherboard[1] = $objItem.Model
        $data_motherboard[2] = $objItem.Product
        $data_motherboard[3] = $objItem.SerialNumber
        $data_motherboard[4] = $objItem.Version
    Next
Else
    $data_motherboard = False
Endif

Is there any way to use a variable in place of the object properties? So instead of....

$objitem.Manufacturer

I'm after something like...

$objitem.$mypropertyname

I've tried it as above, but it doesn't work. Is there any way of doing this?

Thanks,

JT

Link to comment
Share on other sites

Good coffee this morning:

#include <Array.au3>
$sProperty = "Manufacturer"
$objWMIService = ObjGet("WINMGMTS:\\.\ROOT\CIMV2")
$colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_BaseBoard", "WQL")
Dim $data_motherboard[5]
If IsObj($colItems) then
    For $objItem In $colItems
        $sData = Eval("objItem." & $sProperty)
        $data_motherboard[0] = $objItem.Manufacturer
       ; $data_motherboard[0] = $objItem.Manufacturer
        $data_motherboard[1] = $objItem.Model
        $data_motherboard[2] = $objItem.Product
        $data_motherboard[3] = $objItem.SerialNumber
        $data_motherboard[4] = $objItem.Version
    Next
Else
    $data_motherboard[0] = False
Endif

_ArrayDisplay($data_motherboard, "$data_motherboard")

:)

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

This script does work, but not sure what you mean by it!

It's still pulling information from those properties without variables.

:P

My mistake, posted the wrong version. I was trying various things with Eval(), Execute(), etc. Here is the one that worked and should have been posted to begin with:
#include <Array.au3>
$sProperty = "Manufacturer"
$objWMIService = ObjGet("WINMGMTS:\\.\ROOT\CIMV2")
$colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_BaseBoard", "WQL")
Dim $data_motherboard[5]
If IsObj($colItems) then
    For $objItem In $colItems
        $sData = Execute("$objItem." & $sProperty)
        $data_motherboard[0] = $sData
       ; $data_motherboard[0] = $objItem.Manufacturer
        $data_motherboard[1] = $objItem.Model
        $data_motherboard[2] = $objItem.Product
        $data_motherboard[3] = $objItem.SerialNumber
        $data_motherboard[4] = $objItem.Version
    Next
Else
    $data_motherboard[0] = False
Endif

_ArrayDisplay($data_motherboard, "$data_motherboard")

:)

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...