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
How do you define the default/main NIC?
I would say the nic that is associated with the "Local Area
Connection" NetConnectionID.
'/-----------------------------------------------------------------------
>
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
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
urkec,
Thankyou very much - that is simply perfect! works exactly the way i
want.
Thanks again,
Daz
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?
From experience sometimes windows will assign the default NIC "Local
Area Connection2" etc and this could be the reason for not finding
it...
> 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:
Also WMI Code Creator:
--
urkec
> > - 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
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
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
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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