Jump to content

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


Recommended Posts

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

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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
Link to comment
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)
Link to comment
Share on other sites

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
Link to comment
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.

Link to comment
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

Link to comment
Share on other sites

Here's what I needed it for btw.

#include <Array.au3>

Local $oWMIService = _WMIService()  ;Instantiate WMIService Object - Establish Connection
If $oWMIService = 0 Then Exit   ;If WMI Object failed to instantiate, Abort Script

Local $sWMIQueryClass = "Win32_ComputerSystem"  ;Declare WMI Class to Query
Local $aWMIQueryFields[]= ["Name","Manufacturer","Model","Domain"]  ;Declare Fields to Retrieve From WMI Class

$aResults = _WMIQuery($oWMIService,$sWMIQueryClass,$aWMIQueryFields)  ;Run WMI Query against WMIService Object
$oWMIService = 0  ;Termninate WMIService Object

_ArrayDisplay($aResults)    ;Display Results Array
Exit

Func _WMIService($sHost = @ComputerName) ;Connect to WMI Service
    $oWMIService = ObjGet("winmgmts:{impersonationLevel=impersonate}!\\" & $sHost & "\root\cimv2")  ;Create WMI Service Object
    If Not IsObj($oWMIService) Then Return SetError(1,0,0)  ;If Obj Instantiation Failed, Error Out
    Return $oWMIService ;Return WMI Service Object
EndFunc

Func _WMIQuery($oWMIService,$sWMIQueryClass,$aWMIQueryFields) ;Perform WMI Query with Query String and Data Return Parameters
    If Not IsObj($oWMIService) Then Return SetError(1,0,0)  ;If oWMIService is not a valid Object, Error Out
    If Not IsArray($aWMIQueryFields) Then Return SetError(2,0,0)    ;If Query Fields is not a valid array, Error Out

    Local $aResults = $aWMIQueryFields  ;Create Results Array
    _ArrayColInsert($aResults, 1)   ;Add Dimension to Results Array to Hold Query Return Values

    Local $sWMIQuery = "SELECT " & _ArrayToString($aWMIQueryFields,",") & " FROM " & $sWMIQueryClass    ;Create WMI Query String
    $oItems = $oWMIService.ExecQuery ($sWMIQuery)  ;Execute query using WMI Service Object

    For $i = 0 to UBound($aWMIQueryFields) - 1  ;Loop through WMI Query Results
        For $oItem In $oItems
            $aResults[$i][1] = Execute("$oItem." & $aWMIQueryFields[$i])  ;Result
        Next
    Next

    Return $aResults  ;Return Result Array
EndFunc

edit: optimized example

Edited by spudw2k
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...