Jump to content

How to get values from a WMI property that is an Array?


Recommended Posts

I've tried looking through the forums with no luck.

I'm trying to evaluate the values in a WMI Property that is an array... and can't figure out how.

So basically I'm:

Connecting to the Namespace

Executing a Query

Then doing the following code:

For $PropList in $PropLists
    If $PropList.ListName = "Enabled" Then
        For $Value in $PropList.Values
            Switch $Value
            Case "1"
            FileWriteLine($Log, @TAB & "1")
            Case "2"
            FileWriteLine($Log, @TAB & "2")
            Case "3"
            FileWriteLine($Log, @TAB & "3")
            Case Else
            FileWriteLine($Log, $Value & @CRLF)
        EndSwitch
    EndIf
Next

I don't get any errors from my error handler but from testing I found the code just ends when it gets to the "For $Value in $PropList.Values"

From looking in WMI I found that $PropList.Values is an Array.

What do I need to do to get each value from the WMI Property Array one at a time? How do I declare "$PropList.Values" as an array? Do I need to?

In vbscript this code works using following:

For each oPropList in aPropList
     If oPropList.ListName = "Enabled" Then
         For each oValue in oPropList.Values
             Select Case oValue
                 Case "1"
                      WScript.Stdout.Write vbTab & "1"
                 Case "2"
                      WScript.Stdout.Write vbTab & "2"
                 Case "3"
                      WScript.Stdout.Write vbTab & "3"
                 Case Else
                      WScript.Stdout.Write oValue & vbCrLf
              End Select
         Next
     End If
Next

Thanks,

Terry

Link to comment
Share on other sites

When I tried to make a report generator for networking settings, I used this code to get the list of DNS/WINS servers from Win32_NetowrkAdapterConfiguration.

Func _GetArrData($object)
    If IsArray($object) Then
        Local $result = ""
        For $item In $object
            $result &= $item & "|"
        Next
        Return StringSplit(StringTrimRight($result, 1), "|")
    EndIf
    Local $default[1]
    $default[0] = 0
    Return $default
EndFunc

If the passed parameter is an array, it will return an array containing the parameter's contents with the [0] element holding the element count.

Its use goes something like this:

$colItems = _WmiQuery("SELECT DNSServerSearchOrder FROM Win32_NetworkAdapterConfiguration WHERE (Caption = ""SomeCaption"")")
If IsObj($colItems) Then
    For $objItem In $colItems
        $adapters[5] = _GetArrData($objItem.DNSServerSearchOrder )
    Next
EndIf
Edited by omikron48
Link to comment
Share on other sites

When I tried to make a report generator for networking settings, I used this code to get the list of DNS/WINS servers from Win32_NetowrkAdapterConfiguration.

Func _GetArrData($object)
    If IsArray($object) Then
        Local $result = ""
        For $item In $object
            $result &= $item & "|"
        Next
        Return StringSplit(StringTrimRight($result, 1), "|")
    EndIf
    Local $default[1]
    $default[0] = 0
    Return $default
EndFunc

If the passed parameter is an array, it will return an array containing the parameter's contents with the [0] element holding the element count.

Its use goes something like this:

$colItems = _WmiQuery("SELECT DNSServerSearchOrder FROM Win32_NetworkAdapterConfiguration WHERE (Caption = ""SomeCaption"")")
If IsObj($colItems) Then
    For $objItem In $colItems
        $adapters[5] = _GetArrData($objItem.DNSServerSearchOrder )
    Next
EndIf

Thanks!

So I tried incorporating this into my script but since I have two "For" Loops and the second with the property identifier in it I wasn't sure how to translate your:

For $objItem In $colItems

$adapters[5] = _GetArrData($objItem.DNSServerSearchOrder )

Next

Part. Seems like I would maybe need to get rid of the second for loop, use this then do some array reads to write out the right lines?

Terry

Edited by mattw112
Link to comment
Share on other sites

All you really need to pay attention to is this bit here:

_GetArrData($objItem.DNSServerSearchOrder )

Basically, what you want is to pass the array property from your WMI query to _GetArrData and it will spit out an array of its contents.

Edited by omikron48
Link to comment
Share on other sites

This might help:

;Coded by UEZ 2009
#AutoIt3Wrapper_Change2CUI=y
#AutoIt3Wrapper_UseUpx=n

Global $ip = "localhost"
If $CmdLine[0] > 0 Then $ip = $CmdLine[1]

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

MsgBox(0, "Network Adapter Configuration", "DNS Server Search Order: " & GetWMI($ip))

Func GetWMI($srv)
    Local $Net_DNSSearchOrder, $colItems, $colItem, $ping, $x
    $ping = Ping($srv)
    If $ping Then
        $colItems = $objWMIService.ExecQuery("SELECT DNSServerSearchOrder FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True", "WQL", 0x30)
        If IsObj($colItems) Then
            For $objItem In $colItems
                For $x = 0 To UBound($objItem.DNSServerSearchOrder) - 1
                    $Net_DNSSearchOrder &= $objItem.DNSServerSearchOrder($x) & " "
                Next
            Next
            SetError(0)
            Return $Net_DNSSearchOrder
        Else
            SetError(1)
            Return "Error!"
        EndIf
    Else
        SetError(1)
        Return "Host not reachable"
    EndIf
EndFunc

Br,

UEZ

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

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