Jump to content

Need all three - IP, NicStatus, and NicID


ofLight
 Share

Recommended Posts

So I can Get the Status and NetConnectionID(Name of the network connection as it appears in the Network Connections Control Panel program) from Win32_NetworkAdapter and I can Get the IP from Win32_NetworkAdapterConfiguration but I don't see a Way of just getting all 3 from Either one. Currently the only way I see to do it is to grab the IP and ID/Status Separately then match them by MAC address ?? But there has to be a better way.

Ref: MSDN Win32_NetworkAdapter Class

Ref: MSDN Win32_NetworkAdapterConfiguration Class

;;; Grabs IP and MAC
$strComputer = "." 
$objNetwork = ObjCreate("WScript.Network")
$strComputer = $objNetwork.ComputerName
$objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\cimv2")
$colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration") 

For $objItem in $colItems 
    $IP = $objItem.IPAddress(0)
    If $IP <> "" Then
        Consolewrite( "IPAddress: " & $objItem.IPAddress(0)&@CRLF)
        Consolewrite( "MACAddress: " & $objItem.MACAddress&@CRLF)
        Consolewrite( "-----------------------------------"&@CRLF)
    EndIf
Next

;-----------------------------------------------------------------------------
Consolewrite( "-----------------------------------"&@CRLF)
;-----------------------------------------------------------------------------

;;; Grabs ID and MAC and Status
$o_WMIService = ObjGet("winmgmts:\\" & @ComputerName & "\root\CIMV2")
$a_CollectedConnections = $o_WMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapter", "WQL", 0x20 + 0x10)

For $o_ConnectionItem In $a_CollectedConnections
    If $o_ConnectionItem.NetConnectionID <> "" Then

        Consolewrite("ID: " & $o_ConnectionItem.NetConnectionID&@CRLF)
        Consolewrite("Status: " & $o_ConnectionItem.NetConnectionStatus&@CRLF)
        Consolewrite("MACAddress: " & $o_ConnectionItem.MACAddress&@CRLF)
        Consolewrite( "-----------------------------------"&@CRLF)
    EndIf
Next

There is always a butthead in the crowd, no matter how hard one tries to keep them out.......Volly

Link to comment
Share on other sites

Two ways I can think of off the top of my head...

1.) Faster execution (Less WMI calls), but more code: Dump all the info and connect the data by MAC address.

Warning: Insufficient sleep. Try again later.

2.) Slower execution (More WMI calls), but less code: Query the ID/Status for each adapter separately.

;;; Grabs IP and MAC
Local $strComputer = "."
Local $objNetwork = ObjCreate("WScript.Network")
$strComputer = $objNetwork.ComputerName
Local $objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\cimv2")
Local $colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration")
Local $IP, $o_WMIService, $a_CollectedConnections

For $objItem In $colItems
    $IP = $objItem.IPAddress(0)
    If $IP <> "" Then
        ;;; Grabs ID and MAC and Status
        $o_WMIService = ObjGet("winmgmts:\\" & @ComputerName & "\root\CIMV2")
        $a_CollectedConnections = $o_WMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapter", "WQL", 0x20 + 0x10)

        For $o_ConnectionItem In $a_CollectedConnections
            If $o_ConnectionItem.NetConnectionID <> "" And $o_ConnectionItem.MACAddress = $objItem.MACAddress Then
                ConsoleWrite("ID: " & $o_ConnectionItem.NetConnectionID & @CRLF)
                ConsoleWrite("Status: " & $o_ConnectionItem.NetConnectionStatus & @CRLF)
                ConsoleWrite("IPAddress: " & $objItem.IPAddress(0) & @CRLF)
                ConsoleWrite("MACAddress: " & $o_ConnectionItem.MACAddress & @CRLF)
                ConsoleWrite("-----------------------------------" & @CRLF)
            EndIf
        Next
    EndIf
Next

[font="Tahoma"]"Tougher than the toughies and smarter than the smarties"[/font]

Link to comment
Share on other sites

Thanks for your reply Skruge,

I am currently using the equivalent of your second example making the two WMI calls and then matching them by MAC.

I am not sure I understand what u mean by

1.) Faster execution (Less WMI calls), but more code: Dump all the info and connect the data by MAC address.

I thought that is exactly what we were doing in the second option
$o_ConnectionItem.MACAddress = $objItem.MACAddress

As you said, this is a bit slow but functional. I was just hoping there was another faster method available I had overlooked.

There is always a butthead in the crowd, no matter how hard one tries to keep them out.......Volly

Link to comment
Share on other sites

That's my way of getting the network stuff. Not fast, but AllInOne. :)

#include <File.au3>
#Include <array.au3>
Local $array, $t = 'netsh interface ip dump >"' & @ScriptFullPath & '.txt"'
RunWait(@ComSpec & " /c " & $t, "", @SW_HIDE)
$t = _FileReadToArray(@ScriptFullPath & ".txt", $array)
FileDelete(@ScriptFullPath & ".txt")
_ArrayDisplay($array)
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...