Jump to content

Recommended Posts

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
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
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

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...