The Moose and Squirrel Files

September 5, 2008

Detect Physical Network Adapters using WMI

Filed under: Uncategorized — Tags: , , — networknerd @ 2:07 pm

Getting a list of network adapters is easy just execute a wmi query “select * from win32_networkadapter”.  The hard part is determining which adapters are physical adapters.  I actually tackled this problem about three years ago.  My first efforts involved googling furiously to see what everyone else had done.  Not much.  Looked like I was on my own.

I messed around with wbemtest looking at all the adapters returned from the query and looked for any properties that would tell me if they were physical.  As luck would have it I started clicking on the associators to see if any related classes would help.

It’s obvious in hindsight that the answer is that physical adapters use resources (IO, memory or IRQ). Simple no?  Just execute an associators query for every network adapter returned from the original query.  If one of the returned classes is Win32_IRQResource, Win32_DeviceMemoryAddress or Win32_PortResource then the device is physical.   The script below shows how it is done.  As always some caveats apply.  Network cards in notebooks often have a power saving feature that releases the resources.  They magically return of course when the network card gets link or the power is connected to the notebook.  USB network cards don’t show up using this method either.


Option Explicit

'Constants describing associator classes which indicate a physical adapter
const IRQCLASS = "Win32_IRQResource"
const DEVMEMCLASS = "Win32_DeviceMemoryAddress"
const PORTCLASS = "Win32_PortResource"

'constants for wmi asynccontext key
const CONTEXT_KEY = "pnpdeviceid"
'WQL Query constants
const QRY_ALL_NIC = "select * from win32_networkadapter"
const QRY_NIC_ASSOC = "associators of {Win32_NetworkAdapter.DeviceID='"
const QRY_NIC_ASSOC2 = "'}"

Dim WshShell, oExec
dim ethernic, wnic, nic
dim assoc
dim devid
dim boolPhysicalAdapter
dim colNics, colassoc
dim wmisrv_cimv2

Set WshShell = CreateObject("WScript.Shell")

Set wmisrv_cimv2 = GetObject("winmgmts:root/cimv2")
'get a collection of all win32_networkadapters. We query the associators of each nic and see if
'any of the associator classes indicate they are physical adapters ie do they use memory addresses,
'io port or IRQ resources.
set colNics = wmisrv_cimv2.execquery(QRY_ALL_NIC)
for each nic in colNics
  set colassoc = wmisrv_cimv2.execquery(QRY_NIC_ASSOC & nic.DeviceID & QRY_NIC_ASSOC2)
  boolPhysicalAdapter = FALSE
  for each assoc in colassoc
    select case assoc.Path_.Class
      case IRQCLASS, DEVMEMCLASS, PORTCLASS
        wshShell.logevent 4, "Network adapter " & nic.name & " is an ethernet adapter" & vbcrlf _
          & "pnpdeviceid = " & nic.pnpdeviceid
        boolPhysicalAdapter = TRUE
        exit for
      case else
    end select
  next
  if boolPhysicalAdapter = FALSE then
    wshShell.logevent 4, "Network adapter " & nic.name & " ignored!" & vbcrlf _
      & "Not a physical adapter: pnpdeviceid = " & nic.pnpdeviceid
  end if
next

add to del.icio.us :: Add to Blinkslist :: add to furl :: Digg it :: add to ma.gnolia :: Stumble It! :: add to simpy :: seed the vine :: :: :: TailRank :: post to facebook

Advertisements

1 Comment »

  1. Excellent!!, this is perfect and it works fine

    Comment by Srinath — September 24, 2008 @ 1:33 am


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: