Jump to content

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!

 

Link to comment
Share on other sites

  • Moderators

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

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
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...

Link to comment
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)
Link to comment
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
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

×
×
  • Create New...