Hey, Scripting Guy! When I use WMI to query for network cards on my laptop, it comes back with 32 network cards. 32! I am not lying. My laptop does not even have 32 slots, so how does it have 32 network cards in it? How can I find information about the “real” network cards on my laptop? I figure I should only have two: the wired NIC and the wireless NIC, but I am getting all kinds of stupid stuff coming back. Help!
– LB
Hi LB,
So you need help looking for your network interface card. Have you waited until after sunset? That is when you can find a NIC at … (old joke). Speaking of old things, we used to be able to find our real network card by finding the one that was bound to TCP/IP. We would query the Win32_NetworkAdapterConfiguration WMI class, and filter on the IPEnabled property. Using this approach we would have done something like this:
PS C:\> Get-WmiObject -Class Win32_NetworkAdapterConfiguration ` -filter "IPEnabled = $true"
The problem with this methodology these days is that some of the pseudo-adapters you were complaining about so vociferously are alsoIPEnabled. The above command would eliminate many, but not necessarily all of the adapters.
A better approach is to look at the Win32_NetworkAdapter class and query the NetConnectionStatus property. Using this technique, we return only network adapter devices that are actually connected to a network. While it is possible that a pseudo-adapter could sneak under the wire (pun intended), the likelihood is more remote (pun unintended). In this command we will use the Get-WmiObjectPowerShell cmdlet to return all instances of the Win32_NetworkAdapter class on the computer. We then create a table to display the data returned by the NetConnectionStatus property:
PS C:\> Get-WmiObject -Class Win32_NetworkAdapter | Format-Table -Property Name, NetConnectionStatus -AutoSize
The fruit of our labor is somewhat impressive. We have a nice table that details all of the fake and real network adapters on our laptop, as well as the connection status of each. Here is the list from my laptop:
Value | NetConnectionStatus |
WAN Miniport (L2TP) |
Status not reported |
WAN Miniport (PPTP) |
Status not reported |
WAN Miniport (PPPOE) |
Status not reported |
WAN Miniport (IPv6) |
Status not reported |
Intel(R) PRO/1000 PL Network Connection |
2 |
Intel(R) PRO/Wireless 3945ABG Network Connection |
0 |
WAN Miniport (IP) |
Status not reported |
Microsoft 6to4 Adapter |
Status not reported |
Bluetooth Personal Area Network |
Status not reported |
RAS Async Adapter |
Status not reported |
isatap.{51AAF9FF-857A-4460-9F17-92F7626DC420} |
Status not reported |
Virtual Machine Network Services Driver |
Status not reported |
Microsoft ISATAP Adapter |
Status not reported |
Bluetooth Device (Personal Area Network) |
7 |
6TO4 Adapter |
Status not reported |
Microsoft 6to4 Adapter |
Status not reported |
Microsoft Windows Mobile Remote Adapter |
Status not reported |
isatap.launchmodem.com |
Status not reported |
isatap.{647A0048-DF48-4E4D-B07B-2AE0995B269F} |
Status not reported |
Microsoft Windows Mobile Remote Adapter |
Status not reported |
WAN Miniport (SSTP) |
Status not reported |
WAN Miniport (Network Monitor) |
Status not reported |
6TO4 Adapter |
Status not reported |
6TO4 Adapter |
Status not reported |
Microsoft 6to4 Adapter |
Status not reported |
Microsoft Windows Mobile Remote Adapter |
Status not reported |
isatap.{C210F3A1-6EAC-4308-9311-69EADBA00A04} |
Status not reported |
isatap.launchmodem.com |
Status not reported |
Virtual Machine Network Services Driver |
Status not reported |
Virtual Machine Network Services Driver |
Status not reported |
Teredo Tunneling Pseudo-Interface |
Status not reported |
isatap.{647A0048-DF48-4E4D-B07B-2AE0995B269F} |
Status not reported |
There are two things you will no doubt notice. The first is that most of the network adapters report no status whatsoever. The second thing you will notice is that the ones that do report a status do so in some kind of code. The previous table is therefore pretty much useless! But it does look nice.
A little work in the Windows SDK looking up the Win32_NetworkAdapter WMI class, and we run across the following information:
Value | Meaning |
0 |
Disconnected |
1 |
Connecting |
2 |
Connected |
3 |
Disconnecting |
4 |
Hardware not present |
5 |
Hardware disabled |
6 |
Hardware malfunction |
7 |
Media disconnected |
8 |
Authenticating |
9 |
Authentication succeeded |
10 |
Authentication failed |
11 |
Invalid address |
12 |
Credentials required |
Ah, the results of our quest for networking nirvana. The value of 2 means the network adapter is connected. Here is the code we wrote to exploit the results of our research:
Get-WmiObject -class win32_networkadapter -filter "NetConnectionStatus = 2" | format-list -Property [a-z]*
However, such ecstasy is short lived when we realize that, even though we have indeed returned information about a network adapter that is connected, we do not have any of the configuration information from the card. Bummer! Note the results of the query:
What we need is to be able to use the NetConnectionStatus property from Win32_Networkadapter and to be able to obtain the TCP/IP configuration information from the Win32_NetworkAdapterConfiguration WMI class. This sounds like a job for an association class. In VBScript, querying an Association class involved performing confusing AssociatorsOf queries. (Refer to the Microsoft Press book,Microsoft Windows Scripting with WMI: Self-Paced Learning Guide, for more information about this technique.)
Using the Association class with Windows PowerShell, we come up with the script shown here:
Param($computer = "localhost") function funline ($strIN) { $num = $strIN.length for($i=1 ; $i -le $num ; $i++) { $funline = $funline + "=" } Write-Host -ForegroundColor yellow $strIN Write-Host -ForegroundColor darkYellow $funline } #end funline Write-Host -ForegroundColor cyan "Network adapter settings on $computer" Get-WmiObject -Class win32_NetworkAdapterSetting ` -computername $computer | Foreach-object ` { If( ([wmi]$_.element).netconnectionstatus -eq 2) { funline("Adapter: $($_.setting)") [wmi]$_.setting [wmi]$_.element } #end if } #end foreach
We begin the script by using a command-line parameter to allow us to run the script remotely, if needed. We use the Param statement to do this. We also create a function named funline that is used to underline the results of the query. It makes the output nicer if there is a large amount of data returned:
Param($computer = "localhost") function funline ($strIN) { $num = $strIN.length for($i=1 ; $i -le $num ; $i++) { $funline = $funline + "=" } Write-Host -ForegroundColor yellow $strIN Write-Host -ForegroundColor darkYellow $funline } #end funline
We print out the name of the computer by using the Write-Host cmdlet as seen here. We use the color cyan so that the text will show up very nicely on the screen:
Write-Host -ForegroundColor cyan "Network adapter settings on $computer"
Then we get down to the actual WMI query. To do this, we use the Get-WmiObject cmdlet. We use the -computername parameter to allow the script to run against other computers, and we pipeline the results to the ForEach-Object cmdlet:
Get-WmiObject -Class win32_NetworkAdapterSetting ` -computername $computer | Foreach-object `
The hard part of the query is seen here. We need a way to look at the netConnectionStatus property of the Win32_NetworkAdapterclass. This class is referred to by the reference returned from the association query. It is called element. To gain access to this class, we use the reference that was returned and feed it to the [WMI] type accelerator (it likes to receive a path, and this is what the reference is). Because the reference refers to a specific instance of a WMI class, and because the [WMI] type accelerator can query a specific instance of a class, we are now able to obtain the value of the netConenctionStatus property. So we say in our script that if it is equal to 2, we will print out the name of the network adapter, and the configuration that is held in the setting property, and the adapter information that is held in the element property. This section of the code is seen here:
{ If( ([wmi]$_.element).netconnectionstatus -eq 2) { funline("Adapter: $($_.setting)") [wmi]$_.setting [wmi]$_.element } #end if
The result of running the script is that it displays information from both the Win32_NetworkAdapter WMI class and theWin32_NetworkAdapterConfiguration class. It also shows us we only have one connected network adapter. Take a look for yourself:
LB, I hope this script can help you to find the real network adapter on your computer. In addition, you can use this same technique to probe the depths of results from other association classes. I know we will be reviewing some of these classes in the near future. Thanks for the question, and relax. You do not really have 32 network adapters on your computer.
Ed Wilson and Craig Liebendorfer, Scripting Guys
0 comments