Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Determine physical NIC

Status
Not open for further replies.

MasterRacker

New member
Oct 13, 1999
3,343
0
0
US
I'm using the script below on a utility notebook that I need to change address on regularly. It works fine except that it not only canges address on the physical NIC but it also tries to set the virtual NIC installed by Sun VirtualBox.

What's a good way to filter the collection of NICS to only work with physical NICS?

Code:
'===============================================================================
' NAME: MultiSetIP.vbs
' AUTHOR: Jeff Xxxxxxx
' DATE: 02/01/2010
' DESCRIPTION: 
'     Set IP address for Panasonic DVR or Bbb VLAN access.  
'     Command line argument 'D' resets to DHCP.
'     Compiled from various sources
'===============================================================================
strComputer = "."
set objWMIService = GetObject ("winmgmts:" & "{impersonationLevel = impersonate}!\\" & strComputer & "\root\cimv2")
set colAdapters = objWMIService.ExecQuery("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE")

arg = UCase(InputBox("Enter network. (D=DHCP, B=Bbb VLAN, P=Panasonic DVR):")
if arg = "D" then
	SetDHCP
else
	SetStatic(arg)
end if	

sub SetStatic(net)
	useDNS = FALSE
	strGatewayMetric = Array(1)
	arrDNSServers = Array("10.10.1.1", "10.10.1.2")
	
	select case net 
	case "P"
		strIPAddress = Array("192.168.0.11")
		strSubnetMask= Array("255.255.255.0")
		strGateway = Array("192.168.0.1")
	case "B"
		strIPAddress = Array("10.20.10.175")
		strSubnetMask= Array("255.255.0.0")
		strGateway = Array("10.20.10.254")
		useDNS = TRUE
	case else
		Wscript.Echo "Invalid network. USAGE: MultiSetIP <D|B|P> (D=DHCP, B=Bbb VLAN, P=Panasonic DVR)"
		Wscript.Quit
	end select	
		
	for each objAdapter in colAdapters
		errIP = objAdapter.EnableStatic(strIPAddress,strSubnetMask)
		errGateway= objAdapter.SetGateways(strGateway, strGatewayMetric)
		
		if errIP = 0 and errGateway = 0 then
			WScript.Echo "IP address configured."
		elseif errIP = 1 or errGateway = 1 then
			WScript.Echo "You must reboot for the changes to take effect."
		else
			WScript.Echo "Unable to configure IP address. IP: " & errIP & " and Gateway:" & errGateway
		end if
		
		if useDNS then
			errDNS = objAdapter.SetDNSServerSearchOrder(arrDNSServers)
			
			If errDNS = 0 Then
				WScript.Echo " DNS Servers set."
			Else
				WScript.Echo " Error setting DNS server info."
			End If
		end if
	next
end sub

sub SetDHCP()
	for each objAdapter in colAdapters
		errDHCP = objAdapter.EnableDHCP()
	    if errDHCP = 0 then
	        Wscript.Echo "DHCP has been enabled."
	    else
	        Wscript.Echo "DHCP could not be enabled."
	    end if

	    errDNS = objAdapter.SetDNSServerSearchOrder()
	    errDDNS = objAdapter.SetDynamicDNSRegistration
	    errRenew = objAdapter.RenewDHCPLease

	    if errRenew <> 0 then
	        Wscript.Echo "DHCP lease could not be renewed." & err.number & err.description
	    end if
	next
end sub

Jeff
[small][purple]It's never too early to begin preparing for [/purple]International Talk Like a Pirate Day
"The software I buy sucks, The software I write sucks. It's time to give up and have a beer..." - Me[/small]
 
Oh yeah, I'd like to avoid wireless nics as well.

Jeff
[small][purple]It's never too early to begin preparing for [/purple]International Talk Like a Pirate Day
"The software I buy sucks, The software I write sucks. It's time to give up and have a beer..." - Me[/small]
 
Hmmm. You could see if there is a registry discrepency between virtual and physical NICs.
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Class\{4D36E972-E325-11CE-BFC1-08002BE10318}

-Geates
 
So I found a two part article here:
It looks like I need to associate between the NetworkAdapterConfiguration ad NetworkAdapter classes.

Unfortunately it looks like I have a new problem. I'm trying to simply list both classes and I can't get the NetworkAdapter class to list on XP. I get a VBS error 80041017. Both classes list on my Win7 machine. The articles from 2002 so boh classs have to exist in XP.

Code:
'===============================================================================
strComputer = "."
set objWMIService = GetObject ("winmgmts:" & "{impersonationLevel = impersonate}!\\" & strComputer & "\root\cimv2")
set colAdapter = objWMIService.ExecQuery("Select * from Win32_NetworkAdapterConfiguration Where IPEnabled=True")

for each objAdapter in colAdapter
	Wscript.Echo "Caption: " & objAdapter.Caption 
next
Code:
'===============================================================================
strComputer = "."
set objWMIService = GetObject ("winmgmts:" & "{impersonationLevel = impersonate}!\\" & strComputer & "\root\cimv2")
set colAdapter = objWMIService.ExecQuery("Select * from Win32_NetworkAdapter Where PhysicalAdapter=True")

for each objAdapter in colAdapter
	Wscript.Echo "Caption: " & objAdapter.Caption 
next
Note: I tried the second example without the PhysicalAdapter clause and it still fails.

Jeff
[small][purple]It's never too early to begin preparing for [/purple]International Talk Like a Pirate Day
"The software I buy sucks, The software I write sucks. It's time to give up and have a beer..." - Me[/small]
 
OK. In the example above that fails on XP, I found the problem to be that machine. The same script works fine on a different XP box.

I've done more digging and the example below associates te matching objects in each class. I'm posting for those who are interested.
Code:
On Error Resume Next
 
strComputer = "."
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colAdapterCfgs = objWMIService.ExecQuery ("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")
 
For Each objAdapterCfg In colAdapterCfgs
  Set objAdapter = objWMIService.Get ("Win32_NetworkAdapter.DeviceID=" & objAdapterCfg.Index)
  
  wscript.echo "Cfg Caption:" & objAdapterCfg.Caption
  
  if objAdapter.PhysicalAdapter = True then
'	wscript.echo "Caption:" & objAdapter.Caption & " IP:" & objAdapterCfg.IPAddress(0)
	wscript.echo "Caption:" & objAdapter.Caption
	wscript.echo "Physical adapter"
  else	
	wscript.echo "Virtual adapter"
  end if	
Next

As it turns out, on my notebook, the NIC, Wireless and Bluetooh adapters all report that they are physical, so this method was a good learning experience but doesn't do my any good.

It may not be robust, but for my purposes, it looks like the best route is to just check for "Broadcom" in the caption since all the notebooks I will use this on are Dells with Broadcom adapters:

[tt]if InStr (1,objAdapterCfg.Caption,"Broadcom", 1) > 0 then[/tt]

Jeff
[small][purple]It's never too early to begin preparing for [/purple]International Talk Like a Pirate Day
"The software I buy sucks, The software I write sucks. It's time to give up and have a beer..." - Me[/small]
 
Take a look at the .Name (and/or .ProductName) in Win32_Network Adapter. Perhaps an InString looking for "Wireless" in one of those elements?

In my Dell (M6400), both the physical and wireless adapters are Broadcom devices.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top