Jump to content
Sign in to follow this  
DrLarch

Need Help with WMI Query (Win32_NetworkAdapter)

Recommended Posts

Not sure how to do this - been digging in the forums but no exp. with SQL queries. I'm sure this is probably very simple...

What I'm trying to do is lookup the Win32_NetworkAdapter NetConnectionID object based on an IP address. I'm thinking the way to do that would be with two queries, first by looking up the Win32_NetworkAdapterConfiguration MACAddress from the IP and then use the MACAddress to get the Win32_NetworkAdapter NetConnectionID since both Win32_NetworkAdapterConfiguration and Win32_NetworkAdapter have the MAC.

This obviously isn't right:

$colItems = $objWMIService.ExecQuery('SELECT * FROM Win32_NetworkAdapterConfiguration Where $objItem.IPAddress = $IPAddress', "WQL")

Anybody have some good references for structuring such queries or how to do this ?

Thanks!

 

Share this post


Link to post
Share on other sites

Something like this?

$wbemFlagReturnImmediately = "&h10"
$wbemFlagForwardOnly = "&h20"
$IPAddress = "192.168.1.1"

$WMI = ObjGet("winmgmts:\\" & @ComputerName & "\root\CIMV2")
$aItems = $WMI.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration", "WQL", $wbemFlagReturnImmediately + $wbemFlagForwardOnly)

For $element in $aItems
    If $element.IPAddress = $IPAddress Then MsgBox(0, $IPAddress, "Found It!")
Next

Share this post


Link to post
Share on other sites

Hrm :blink: Doesn't work for me (Win7-64). I can get the IP and MAC this way:

$wbemFlagReturnImmediately = 0x10
$wbemFlagForwardOnly = 0x20
$colItems = ""
$strComputer = "localhost"

$Output=""
$objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")
$colItems = $objWMIService.ExecQuery('SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True', "WQL", _
                                          $wbemFlagReturnImmediately + $wbemFlagForwardOnly)

If IsObj($colItems) then
   For $objItem In $colItems
      $strIPAddress = $objItem.IPAddress(0)
      $Output = $Output & "IPAddress: " & $strIPAddress & @CRLF
      $Output = $Output & "MACAddress: " & $objItem.MACAddress & @CRLF
      if Msgbox(1,"WMI Output",$Output) = 2 then ExitLoop
      $Output=""
   Next
Else
   Msgbox(0,"WMI Output","No WMI Objects Found for class: " & "Win32_NetworkAdapterConfiguration" )
Endif

But just need to figure out how to then cross ref to the Win32_NetworkAdapter NetConnectionID object...

Share this post


Link to post
Share on other sites

:thumbsup:

I think I got it now (thanks for your feedback JLogan3o13).

#include <Array.au3>

Global $ConnInfo[1][3]
$ConnInfo[0][0] = 0
;_ArrayDisplay($ConnInfo)

$wbemFlagReturnImmediately = 0x10
$wbemFlagForwardOnly = 0x20
$colItems = ""
$strComputer = "localhost"

$Output = ""
$objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")
$colItems = $objWMIService.ExecQuery('SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True', "WQL", _
        $wbemFlagReturnImmediately + $wbemFlagForwardOnly)


If IsObj($colItems) Then
    $i = 1
    For $objItem In $colItems
        ReDim $ConnInfo[$i + 1][3]
        $ConnInfo[$i][0] = $objItem.IPAddress(0)
        $ConnInfo[$i][1] = $objItem.MACAddress
        $i = $i + 1
        $ConnInfo[0][0] += 1
    Next
EndIf

;~ _ArrayDisplay($ConnInfo)

For $i = 1 To $ConnInfo[0][0]

    $objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")
    $colItems = $objWMIService.ExecQuery('SELECT * FROM Win32_NetworkAdapter WHERE MACAddress = "' & $ConnInfo[$i][1] & '"', "WQL", _
            $wbemFlagReturnImmediately + $wbemFlagForwardOnly)

    If IsObj($colItems) Then
        For $objItem In $colItems
            $ConnInfo[$i][2] = $objItem.NetConnectionID
        Next
    EndIf

Next

_ArrayDisplay($ConnInfo)

Share this post


Link to post
Share on other sites

Hum I get an empty col 2 in the _Arraydisplay..

This one works (for me)

$Services = ObjGet("winmgmts:\\.\root\CIMV2") 
$NACs = $Services.ExecQuery('SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True') 
For $NAC In $NACs    
     $NAs = $Services.ExecQuery('SELECT * FROM Win32_NetworkAdapter')
     For $NA In $NAs
     If  $NA.MACAddress = $NAC.MACAddress  Then 
        Msgbox(0,"", $NAC.IPAddress(0) &@CRLF& $NA.MACAddress &@CRLF& $NA.NetConnectionID )
        Exitloop 2
     EndIf
Next
Next

Edit

Got it :thumbsup:

Change the end of your script like this :

If IsObj($colItems) Then
        For $objItem In $colItems
            $ConnInfo[$i][2] = $objItem.NetConnectionID
            Exitloop    ;<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
        Next
    EndIf
Edited by mikell

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
Sign in to follow this  

  • Similar Content

    • By jresine
      Hello, is it possible to know via a script or command, to have the percentage of disk usage of a process? thank you in advance.
      ps: see image

    • By Dwalfware
      HI
      Another head scratched
      Anyone solve how to get the IPV6 from WMI?
      I followed the usual SELECT * FROM Win32_NetworkAdapter WHERE NetConnectionID='" & GUICtrlRead($cboAdapters) & "'
      get the index 
      port it to 
      Local $query = $objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE Index = " & _GetSelectedNIC(), "WQL", 0x30 )
      All i can see is IPv4 addresses
      My question is is there another place the IPv6 information is hiding? I configured my LAN with IPV6 and my wireless with IPv4 so I can see what shows.
      Thanks in Advance.
      PS. I did try google
       
    • By Dwalfware
      Hi Folks
      Can not get my head around this one.
      This function gets the DNS object s
      If you have two DNS entries fine. but if you have only one! Trouble
      $objItem.DNSServerSearchOrder[0] = Primary DNS
      $objItem.DNSServerSearchOrder[1] = Secondry DNS not always there. I need a way to return 0 if not there or the IP if its there.
      Or is there a way to tell how many objects there are in the "[?]"
      I'm terrible at WMI errors, thanks for help in advance.
      Func _GETcurrentNICinfo2() $oErrors = ObjEvent("AutoIt.Error", "Error_Handle") Local $DnsSecondry Local $objWMIService = ObjGet( "winmgmts:\\" & @ComputerName & "\root\CIMV2" ) Local $query = $objWMIService.ExecQuery("SELECT DNSServerSearchOrder FROM Win32_NetworkAdapterConfiguration WHERE Index = " & _GetSelectedNIC(), "WQL", 0x30 ) If @error Then Return 0 If NOT IsObj($query) Then Return 0 Else For $objItem In $query Return $objItem.DNSServerSearchOrder[1] next Endif EndFunc  
    • By Skeletor
      Hi All,
      Here's a really simple question. 
      I ran the code from the helpfile under: _SQLite_Open

      Issue is I end up with an error message: SQLite3.dll Can't be Loaded!
      I placed the *.dll in the include folder, but still nothing. 

      Where must this file be placed.
       
    • By amin84
      Hello,
      I'm having trouble getting information from Win32_Processor:
      https://docs.microsoft.com/en-us/windows/desktop/CIMWin32Prov/win32-processor
      How does this WMI Query work? Let's say I need to query VMMonitorModeExtensions, Level, ProcessorId, and ProcessorType.
      How would this work?
      Thanks.
×
×
  • Create New...