Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Find Default Network Card NIC

28 views
Skip to first unread message

Daz

unread,
May 17, 2008, 4:58:49 AM5/17/08
to
Hi all,

I need a little help with some scripting so i can out put the default
network card being used on any given computer.

I know i can use something like:

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer)
Set colItems =
objWMIService.InstancesOf("Win32_NetworkAdapterConfiguration")
For Each objItem in colItems
Wscript.Echo objItem.Description
Next

However this lists all the NIC's and all i want is the default (main)
NIC.

Any help would be very appreciated...

Cheers,
Daz

Pegasus (MVP)

unread,
May 18, 2008, 4:44:16 AM5/18/08
to

"Daz" <darren....@gmail.com> wrote in message
news:9f5d21ce-c31e-4965...@z24g2000prf.googlegroups.com...

How do you define the default/main NIC?


Daz

unread,
May 18, 2008, 5:47:00 AM5/18/08
to
On May 18, 4:44 pm, "Pegasus \(MVP\)" <I....@fly.com.oz> wrote:
> "Daz" <darren.black...@gmail.com> wrote in message

I would say the nic that is associated with the "Local Area
Connection" NetConnectionID.

Daz

unread,
May 18, 2008, 5:50:28 AM5/18/08
to
The code i have been working with is below... the only problem i have
left to resolve is getting the correct IP address to match the
MacAddress and NIC. If you have more than one NIC (hardware or
software) with this code you get the last ip address :(


'/-----------------------------------------------------------------------
>
Set colItems = ObjWMI.ExecQuery("Select IPAddress, MACAddress
from Win32_NetworkAdapterConfiguration Where IPEnabled=TRUE")

'/-----------------------------------------------------------------------


>
For Each objItem in colItems

If Not IsNull(objItem.IPAddress) Then
For i=LBound(objItem.IPAddress) To UBound(objItem.IPAddress)
IP = objItem.IPAddress(i)
Next
End If
Next

'/-----------------------------------------------------------------------
>
Set colItems = ObjWMI.ExecQuery( "SELECT * FROM
Win32_NetworkAdapter WHERE NetConnectionID = 'Local Area Connection'")

'/-----------------------------------------------------------------------
>
Cnt = 1


For Each objItem in colItems

If Cnt = 1 Then
C1 = C1 +1
Ts.WriteLine "Line" & C1 & "=" & TitleL & "NETWORK ADAPTER
INFORMATION" & TitleR
C1 = C1 +1
Ts.WriteLine "Line" & C1 & "=" & S_3 & "Adapter Name " &
vbTab & objItem.Name
C1 = C1 +1
Ts.WriteLine "Line" & C1 & "=" & S_3 & "Manufacturer By " &
vbTab & objItem.Manufacturer
C1 = C1 +1
Ts.WriteLine "Line" & C1 & "=" & S_3 & "IP Address " &
vbTab & IP
C1 = C1 +1
Ts.WriteLine "Line" & C1 & "=" & S_3 & "MACAddress " &
vbTab & objItem.MACAddress
C1 = C1 +1
Ts.WriteLine "Line" & C1 & "="
Exit For
End If
Next
Cnt = 0

urkec

unread,
May 18, 2008, 8:14:01 AM5/18/08
to
"Daz" wrote:


You can start from Win32_NetworkAdapter and get the matching
Win32_NetworkAdapterConfiguration instance:


strComputer = "."

Set objWMI = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _
& strComputer & "\root\cimv2")

Set colNICs = ObjWMI.ExecQuery _
("Select * From Win32_NetworkAdapter " & _
"Where NetConnectionID = 'Local Area Connection'")

For Each objNIC in colNICs

Set objNICConfig = objWMI.Get _
("Win32_NetworkAdapterConfiguration.Index=" _
& objNIC.Deviceid)

strIP = ""

For Each strIPAddr In objNICConfig.IPAddress
strIP = strIP & strIPAddr & " "
Next

WScript.Echo objNIC.Name
WScript.Echo objNIC.Manufacturer
WScript.Echo strIP
WScript.Echo objNIC.MACAddress

Next


--
urkec

Daz

unread,
May 18, 2008, 10:50:01 AM5/18/08
to

urkec,

Thankyou very much - that is simply perfect! works exactly the way i
want.

Thanks again,
Daz

Monitor

unread,
May 18, 2008, 12:41:09 PM5/18/08
to

"urkec" <ur...@discussions.microsoft.com> wrote in message
news:F1B9D48F-F8F7-4544...@microsoft.com...

Nice solution! Two questions:
- I copied your code verbatim to a .vbs file, without making any
changes, then ran it on two machines. On one it ran flawlessly,
on the other it generated an error message for this line of code:

For Each strIPAddr In objNICConfig.IPAddress

nic.vbs(19, 4) Microsoft VBScript runtime error: Object not a collection

As I said, the code is identical on the two machines. Do you have
a suggestion why objNICConfig.IPAddress might not be a collection
on one machine?

- Where can I look up all this useful WMI stuff?


Daz

unread,
May 18, 2008, 2:09:11 PM5/18/08
to

From experience sometimes windows will assign the default NIC "Local
Area Connection2" etc and this could be the reason for not finding
it...

urkec

unread,
May 18, 2008, 3:16:04 PM5/18/08
to
"Monitor" wrote:

> Nice solution! Two questions:
> - I copied your code verbatim to a .vbs file, without making any
> changes, then ran it on two machines. On one it ran flawlessly,
> on the other it generated an error message for this line of code:
>
> For Each strIPAddr In objNICConfig.IPAddress
>
> nic.vbs(19, 4) Microsoft VBScript runtime error: Object not a collection
>
> As I said, the code is identical on the two machines. Do you have
> a suggestion why objNICConfig.IPAddress might not be a collection
> on one machine?
>
> - Where can I look up all this useful WMI stuff?
>
>
>
>
>

You will receive that error if objNICConfig.IPAddress is Null, you can check
that before trying to enumerate the collection:

If Not IsNull (objNICConfig.IPAddress) Then


For Each strIPAddr In objNICConfig.IPAddress
strIP = strIP & strIPAddr & " "
Next

Else
strIP = "Null"
End If


For information about WMI you can start from the Microsoft Script Center
(links like: Script Repository, Scriptomatic, Windows 2000 Scripting Guide,
Hey Scripting Guy, WMI SDK)

http://www.microsoft.com/technet/scriptcenter/default.mspx

CIM Studio is useful:

http://www.microsoft.com/downloads/details.aspx?familyid=6430F853-1120-48DB-8CC5-F2ABDC3ED314&displaylang=en

Also WMI Code Creator:

http://www.microsoft.com/downloads/details.aspx?FamilyID=2cc30a64-ea15-4661-8da4-55bbc145c30e&displaylang=en


--
urkec

urkec

unread,
May 18, 2008, 3:29:12 PM5/18/08
to
"Daz" wrote:

> > - I copied your code verbatim to a .vbs file, without making any
> > changes, then ran it on two machines. On one it ran flawlessly,
> > on the other it generated an error message for this line of code:
> >
> > For Each strIPAddr In objNICConfig.IPAddress
> >
> > nic.vbs(19, 4) Microsoft VBScript runtime error: Object not a collection
> >

> From experience sometimes windows will assign the default NIC "Local


> Area Connection2" etc and this could be the reason for not finding
> it...
>


It can also be changed to something else, maybe it is better not to rely on
it. You could start from this script instead:

http://www.microsoft.com/technet/scriptcenter/scripts/network/client/list/nwlsvb01.mspx

--
urkec

Monitor

unread,
May 18, 2008, 5:41:16 PM5/18/08
to

"urkec" <ur...@discussions.microsoft.com> wrote in message
news:C2D18043-AFC9-4AC0...@microsoft.com...
> urkec

Thanks for the very useful information on WMI and CIM Studio.

On my problem machine I had some 20 devices that WMI would consider
as Win32_NetworkAdapters. Their descriptions are all over the place, e.g.
Broadcom NetXtreme Fast Ethernet, WAN Miniport (L2TP) or
D-Link AirPlus G DWL-G122 Wireless USB Adapter. "Local Area
Connection" was not one of them. This forced me to change the WMI
query statement to
Where NetConnectionID > ''
For reasons that I do not understand, the statement
Where NetConnectionID <> ''
failed to exclude blank NetConnectionIDs.

I then followed your suggestion of checking if objNICConfig.IPAddress
is a Null variable. The full code follows.

Set objWMI = GetObject("winmgmts:" _

& "{impersonationLevel=impersonate}!\\.\root\cimv2")

Set colNICs = objWMI.ExecQuery _
("Select * From Win32_NetworkAdapter Where NetConnectionID > ''")

For Each objNIC In colNICs


Set objNICConfig = objWMI.Get _

("Win32_NetworkAdapterConfiguration.Index=" & objNIC.Deviceid)

If Not IsNull(objNICConfig.IPAddress) Then
strIP = ""


For Each strIPAddr In objNICConfig.IPAddress
strIP = strIP & strIPAddr & " "
Next

WScript.Echo objNIC.Name


WScript.Echo objNIC.Manufacturer
WScript.Echo strIP
WScript.Echo objNIC.MACAddress

WScript.echo
End If
Next


James Whitlow

unread,
May 18, 2008, 7:50:49 PM5/18/08
to
"Monitor" <nos...@spam.com> wrote in message
news:uQi07XQu...@TK2MSFTNGP04.phx.gbl...

>
> Nice solution! Two questions:
> - I copied your code verbatim to a .vbs file, without making any
> changes, then ran it on two machines. On one it ran flawlessly,
> on the other it generated an error message for this line of code:
>
> For Each strIPAddr In objNICConfig.IPAddress
>
> nic.vbs(19, 4) Microsoft VBScript runtime error: Object not a collection

Are all of your computers on a single subnet? If so, you can search for
the adapter within your subnet. See code below. If they are not, but are
contained within a single network, the code could be modified to set a
larger subnet mask outside of the loop.

'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
sNetwork = "10.0.0.0"

strComputer = "."
Set objWMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set colItems = ObjWMI.ExecQuery("Select * from " _
& "Win32_NetworkAdapterConfiguration Where IPEnabled=TRUE")

For Each objItem in colItems

aIP = Split(objItem.IPAddress(0), ".")
aSM = Split(objItem.IPSubnet(0), ".")
sSubnet = Empty
For i = 0 to 3
sSubnet = sSubnet & (aIP(i) And aSM(i))
If i < 3 Then sSubnet = sSubnet & "."
Next
If sSubnet = sNetwork Then Exit For Else aIP = Null
Next

Select Case IsNull(aIP)
Case False MsgBox Join(aIP, ".")
Case True MsgBox "No Match NIC Found!", vbCritical
End Select
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


urkec

unread,
May 19, 2008, 12:20:05 PM5/19/08
to
"Monitor" wrote:


NetConnectionID can be changed and it wont't work in Windows 2000, better
use Win32_NetworkAdapterConfiguration.IPEnabled = True, like J. Whitlow
showed:


strComputer = "."

Set objWMI = GetObject("winmgmts:" _

& "{impersonationLevel=impersonate}!\\" _
& strComputer & "\root\cimv2")

Set colNICConfig = objWMI.ExecQuery _
("Select * From Win32_NetworkAdapterConfiguration " _
& "Where IPEnabled = True")

For Each objNICConfig In colNICConfig

strIP = ""

If Not IsNull (objNICConfig.IPAddress) Then
For Each strIPAddr In objNICConfig.IPAddress
strIP = strIP & strIPAddr & " "
Next
Else
strIP = "Null"
End If

Set colNICs = _
objNICConfig.Associators_("Win32_NetworkAdapterSetting")

For Each objNIC In colNICs

WScript.Echo objNIC.Name
WScript.Echo objNIC.Manufacturer
WScript.Echo strIP
WScript.Echo objNIC.MACAddress

WScript.Echo
Next

Next


I also used Associators_ instead of Get to get Win32_NetworkAdapter instance.

--
urkec

0 new messages