MattHiggs

(solved) Array of objects question

6 posts in this topic

#1 ·  Posted (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 by MattHiggs

Share this post


Link to post
Share on other sites



#2 ·  Posted

You can try:

$colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True").ItemIndex(0)
$hol = $colItems.Description

 

1 person likes this

Share this post


Link to post
Share on other sites

#3 ·  Posted (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 by MattHiggs

Share this post


Link to post
Share on other sites

#4 ·  Posted

Weird because it works for me, Windows 10 x64 Enterprise.

Share this post


Link to post
Share on other sites

#5 ·  Posted (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 by MattHiggs

Share this post


Link to post
Share on other sites

#6 ·  Posted

2017-05-18 19_25_13-C__Users_whiggs_Documents_GitHub_wol_solution_WOL.au3 - SciTE [2 of 2].png

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