Sign in to follow this  
Followers 0
rajeshontheweb

How do i use a variable as parameter name for object

3 posts in this topic

#1 ·  Posted (edited)

here is my situation

i have an object (actually wmi object)

Func TestFunction($Parameter)
    Dim $wbemFlagReturnImmediately = 0x10
    Dim $wbemFlagForwardOnly = 0x20
    Dim $colItems = ""
    Dim $strComputer = "localhost"
    Dim $retVal = 0

    $objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")
    $colItems = $objWMIService.ExecQuery("SELECT * FROM win32_computersystemproduct", "WQL", $wbemFlagReturnImmediately + $wbemFlagForwardOnly)

    If IsObj($colItems) Then

        For $objItem In $colItems
            $retVal = "Caption: " & $objItem.UUID ; <<<<<<<<<<<<<<<<<<<<<<<<<<------------------
        Next

    EndIf

    Return $retVal

EndFunc   ;==>TestFunction


ConsoleWrite(@CRLF & TestFunction("UUID") & @CRLF)

Now, In place of calling TestFunction("UUID") if i say "Name" it should execute the line as $objItem.Name

i.e.,

TestFunction("UUID") <--> $retVal = "Caption: " & $objItem.UUID
TestFunction("Name") <--> $retVal = "Caption: " & $objItem.Name
Edited by rajeshontheweb

Share this post


Link to post
Share on other sites



ConsoleWrite(TestFunction("UUID") & @CRLF)
ConsoleWrite(TestFunction("Name") & @CRLF)

Func TestFunction($Parameter)
    Dim $wbemFlagReturnImmediately = 0x10
    Dim $wbemFlagForwardOnly = 0x20
    Dim $colItems = ""
    Dim $strComputer = "localhost"
    Dim $retVal = 0

    $objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")
    $colItems = $objWMIService.ExecQuery("SELECT * FROM win32_computersystemproduct", "WQL", $wbemFlagReturnImmediately + $wbemFlagForwardOnly)

    If IsObj($colItems) Then

        For $objItem In $colItems
            $retVal = "Caption: " & Execute("$objItem." & $Parameter) ; <<<<<<<<<<<<<<<<<<<<<<<<<<------------------
        Next

    EndIf

    Return $retVal

EndFunc   ;==>TestFunction

Share this post


Link to post
Share on other sites

ConsoleWrite(TestFunction("UUID") & @CRLF)
ConsoleWrite(TestFunction("Name") & @CRLF)

Func TestFunction($Parameter)
    Dim $wbemFlagReturnImmediately = 0x10
    Dim $wbemFlagForwardOnly = 0x20
    Dim $colItems = ""
    Dim $strComputer = "localhost"
    Dim $retVal = 0

    $objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")
    $colItems = $objWMIService.ExecQuery("SELECT * FROM win32_computersystemproduct", "WQL", $wbemFlagReturnImmediately + $wbemFlagForwardOnly)

    If IsObj($colItems) Then

        For $objItem In $colItems
            $retVal = "Caption: " & Execute("$objItem." & $Parameter) ; <<<<<<<<<<<<<<<<<<<<<<<<<<------------------
        Next

    EndIf

    Return $retVal

EndFunc   ;==>TestFunction

Hello Authenticity,

That is a nice solution and one I have been using. But I was wondering if there is an alternative to using the "Execute" function, as in the following line:

$retVal = "Caption: " & Execute("$objItem." & $Parameter) ;

Many thanks to anyone who can enlighten me.

taurus905


"Never mistake kindness for weakness."-- Author Unknown --"The highest point to which a weak but experienced mind can rise is detecting the weakness of better men."-- Georg Lichtenberg --Simple Obfuscator (Beta not needed.), Random names for Vars and Funcs

Share this post


Link to post
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
Sign in to follow this  
Followers 0