MattHiggs Posted May 18, 2017 Share Posted May 18, 2017 (edited) Hey Autoit Scripters, So I had a question concerning arrays containing objects. Lets say that you have the following script: $val = test ( "myserver" ) Msgbox ( 1, "", $val ) Func test ( $compname ) $colItems = "" $objWMIService = "" $error = 0 $hol = "" $objWMIService = ObjGet("winmgmts:\\" & $compname & "\ROOT\CIMV2") If Not IsObj ( $objWMIService ) Then $error = 1 Else $colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True") $hol = $colItems[0].Description ;I know this part will result in an error, but it represents what I am trying to accomplish EndIf SetError ( $error ) Return $hol EndFunc As you see above, the $hol variable, as it is, will return an error. I already know that, for what I am creating the script to do, the array of objects in $colItems is only going to contain one object, and even if it doesn't, I am only interested in the first object in the array. My question is as follows: is there a way to reference a single object within an array of objects like you are able to reference a value within an array of values by specifying the index at which the value is located in the array? Or can I only access the objects within an array of objects by looping through the array with a For...In loop? Thanks in advance... Edited May 18, 2017 by MattHiggs Link to comment Share on other sites More sharing options...
Subz Posted May 18, 2017 Share Posted May 18, 2017 You can try: $colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True").ItemIndex(0) $hol = $colItems.Description MattHiggs 1 Link to comment Share on other sites More sharing options...
MattHiggs Posted May 18, 2017 Author Share Posted May 18, 2017 (edited) 2 minutes ago, Subz said: You can try: $colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True").ItemIndex(0) $hol = $colItems.Description I did that as well, as I noticed that it worked in powershell when I ran the powershell equivalent of the command, but not so in autoit. Autoit generated an error. Edited May 18, 2017 by MattHiggs Link to comment Share on other sites More sharing options...
Subz Posted May 18, 2017 Share Posted May 18, 2017 Weird because it works for me, Windows 10 x64 Enterprise. Link to comment Share on other sites More sharing options...
MattHiggs Posted May 18, 2017 Author Share Posted May 18, 2017 (edited) 18 minutes ago, Subz said: Weird because it works for me, Windows 10 x64 Enterprise. Hmmm. Are you querying your local machine? The script that I am writing (and includes the above function) is being run from windows 10 pro, but is querying information from Windows server 2012 R2. Could that have an impact? edit: I'm an idiot... I didn't see the last part that you had appended to the $colItems variable. When I added it, it worked perfectly. Thanks for the help. Edited May 18, 2017 by MattHiggs Link to comment Share on other sites More sharing options...
MattHiggs Posted May 18, 2017 Author Share Posted May 18, 2017 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now