Sign in to follow this  
Followers 0
spudw2k

Is it possible to use a varible as a property or method?

13 posts in this topic

Is it possible to use a variable in place of property or method of an object?

example. I want to do a WMI query and pull variable results.

$host = "localhost"

$strWMIQuery = "SELECT * FROM Win32_ComputerSystem"

$objWMIService = ObjGet("winmgmts:{impersonationLevel=impersonate}!\\" & $host & "\root\cimv2")

$colItems = $objWMIService.ExecQuery ($strWMIQuery)

For $objItem in $colItems

msgbox(0,"Hostname",$objItem.Name) ;<--- .Name is what I'd like to be able replace with a variable

Next

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Is it possible to use a variable in place of property or method of an object?

example. I want to do a WMI query and pull variable results.

$host = "localhost"

$strWMIQuery = "SELECT * FROM Win32_ComputerSystem"

$objWMIService = ObjGet("winmgmts:{impersonationLevel=impersonate}!\\" & $host & "\root\cimv2")

$colItems = $objWMIService.ExecQuery ($strWMIQuery)

For $objItem in $colItems

msgbox(0,"Hostname",$objItem.Name) ;<--- .Name is what I'd like to be able replace with a variable

Next

This is kind of a silly question... what happens when you run it...? :)

And silly questions do not become less silly by liberal application of color tags... ;)

Edited by PsaltyDS

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

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

in short No, it's not possible to do this..

plus I would really disadvice this practise .. (not good coding and allow me to explain)

a method (.Name) is allready the easiest discription of the action you want to perform with this object.

the only thing you could achieve is add unnecessary code (meaning: complications) to your script.

allow me to demonstrate using the Speech api.

speak("this is steven hawkins, wazaaaaaaap")
speak2("this is steven hawkins, wazaaaaaaap")

Func speak($Text)
$spApi = ObjCreate("SAPI.SpVoice")
$spApi.Speak($Text)
EndFunc ;speak($Text)



Func speak2($Text)
$x = "Speak"
$spApi = ObjCreate("SAPI.SpVoice")
$spApi.$x($Text)
EndFunc ;speak($Text)

if you remove the line that calls speak2() and remove the function (speak2)

then the script works.. plus read the errormessage you get when you try running this code..

==> Expected a "=" operator in assignment statement.:

Edited by lordofthestrings

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

A message box with your hostname in it

Well, what I meant was: what happens when you replace .Name with a variable. I was only pointing out that it would be a very easy experiment to conduct and see the results. Looks like lordofthestrings did that for you already, though.

:)

EDIT: This works:

$host = "localhost"
$strWMIQuery = "SELECT * FROM Win32_ComputerSystem"

$objWMIService = ObjGet("winmgmts:{impersonationLevel=impersonate}!\\" & $host & "\root\cimv2")
$colItems = $objWMIService.ExecQuery ($strWMIQuery)
$Property = 'Name'
For $objItem In $colItems
    MsgBox(0, "Hostname", Execute('$objItem.' & $Property))
Next

;)

Edited by PsaltyDS

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

Share this post


Link to post
Share on other sites

ok, that makes me shut up (for a short while again) :)


Share this post


Link to post
Share on other sites

This works for for me:

speak("hello")
speak2("hello")

Func speak($Text)
    $spApi = ObjCreate("SAPI.SpVoice")
    $spApi.Speak($Text)
EndFunc ;speak($Text)



Func speak2($Text)
    $x = "Speak"
    $spApi = ObjCreate("SAPI.SpVoice")
    ;$spApi.$x($Text)
    Execute("$spApi." & $x & "($Text)")
EndFunc ;speak($Text)

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

Does this work for you?

$method = "Name"
$host = "localhost"
$strWMIQuery = "SELECT * FROM Win32_ComputerSystem"

$objWMIService = ObjGet("winmgmts:{impersonationLevel=impersonate}!\\" & $host & "\root\cimv2")
$colItems = $objWMIService.ExecQuery ($strWMIQuery)

For $objItem in $colItems
msgbox(0,"Hostname",Execute("$objItem." & $method))
Next

Not Tested, but should work in theory

EDIT: Whoops, didn't see PsaltyDS' post...

Edited by Senton-Bomb

Share this post


Link to post
Share on other sites

I don't really see a usefullness for this considering that an object has a static set of member functions. They are not dynamic.

Share this post


Link to post
Share on other sites

Well the only reason i was hoping to do this was to be able to call a function and dynamically request particular data, which happens to be returned from a property. I think the main issue is i'm passing a string to the object. Thanks for the answer though. At least I didn't waste to much time trying to find the answer. Maybe I'll just have the function grab all the data I would need and just pick and choose what I need at the time. Thanks again.

Share this post


Link to post
Share on other sites

It sounds like you are giving up... did you misunderstand? An answer was provided... the key is the Execute function:

$Name = "whatever-you-want"
msgbox(0, "Hostname", Execute("$objItem." & $Name))

Dale


Free Internet Tools: DebugBar, AutoIt IE Builder, HTTP UDF, MODIV2, IE Developer Toolbar, IEDocMon, Fiddler, HTML Validator, WGet, curl

MSDN docs: InternetExplorer Object, Document Object, Overviews and Tutorials, DHTML Objects, DHTML Events, WinHttpRequest, XmlHttpRequest, Cross-Frame Scripting, Office object model

Automate input type=file (Related)

Alternative to _IECreateEmbedded? better: _IECreatePseudoEmbedded  Better Better?

IE.au3 issues with Vista - Workarounds

SciTe Debug mode - it's magic: #AutoIt3Wrapper_run_debug_mode=Y Doesn't work needs to be ripped out of the troubleshooting lexicon. It means that what you tried did not produce the results you expected. It begs the questions 1) what did you try?, 2) what did you expect? and 3) what happened instead?

Reproducer: a small (the smallest?) piece of stand-alone code that demonstrates your trouble

Share this post


Link to post
Share on other sites

#13 ·  Posted (edited)

Here's what I needed it for btw.

#include <Array.au3>  ;only needed for demo

$objWMIService = WMIService(@ComputerName)  ;WMIService Object - Establish Connection
If $objWMIService = 0 Then Exit

Local $strData[4]= ["Name","Manufacturer","Model","Domain"]  ;Data to Return from WMI Query

$arrResults = WMIQuery($objWMIService,"SELECT * FROM Win32_ComputerSystem",$strData)  ;Run WMI Query against WMIService Object
$objWMIService = 0  ;Termninate WMIService Object

_ArrayDisplay($arrResults)

Func WMIService($host) ;Connects to WMI Service
    $objWMIService = ObjGet("winmgmts:{impersonationLevel=impersonate}!\\" & $host & "\root\cimv2")
    If not IsObj($objWMIService) Then return 0
    return $objWMIService
EndFunc

Func WMIQuery($objWMIService,$strWMIQuery,$arrData) ;Perform WMI Query with Query String and Data Return Parameters
    If not IsArray($arrData) then return 0
    Local $arrResults[UBound($arrData)][2] ;Two-Dimension Array to store query Results
    $colItems = $objWMIService.ExecQuery ($strWMIQuery)  ;Execute query against WMI Service Object

    For $i = 0 to UBound($arrData) - 1  ;Loop through WMI Query Results
        For $objItem in $colItems
            $arrResults[$i][0] = $arrData[$i]  ;Property/Data Name
            $arrResults[$i][1] = Execute("$objitem." & $arrData[$i])  ;Result
        Next
    Next

    return $arrResults  ;Return Result Array
EndFunc

edit: optimized example

Edited by spudw2k

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