Go Back   TechArena Community > Technical Support > Computer Help > Windows Server > Windows Server Help
Become a Member!
Forgot your username/password?
Register Tags Active Topics RSS Search Mark Forums Read SiteMap

Tags: , ,

Sponsored Links



disable NIC based on connection state

Windows Server Help


Reply
 
Thread Tools Search this Thread
  #1  
Old 13-08-2009
Joey
 
Posts: n/a
disable NIC based on connection state

Is there a script that will disable a nic based on the connection state?
Reply With Quote
  #2  
Old 13-08-2009
Pegasus [MVP]
 
Posts: n/a
I am creating a sysprep image for 2003 server. upon bootup, I want to run a
runonce script that will disable any nics that does not have a "connected"
state. I know how to disable it with netsh but not sure how to detect the
"state" of the nic
Reply With Quote
  #3  
Old 15-08-2009
Pegasus [MVP]
 
Posts: n/a
Re: disable NIC based on connection state

Here you go:

iEthernet = 0: iWireless = 9
aStatus = Split("Disconnected/Connecting/Connected/Disconnecting/" _
& "Hardware not present/Hardware disabled/Hardware malfunction/" _
& "Media disconnected/Authenticating/Authentication succeeded/" _
& "Authentication failed", "/")
Set oWMIService = GetObject("winmgmts:\\.\root\CIMV2")
Set colItems = oWMIService.ExecQuery( _
"SELECT * FROM Win32_NetworkAdapter",,48)
For Each oItem In colItems
If (oItem.AdapterTypeId = iEthernet _
Or oItem.AdapterTypeId = iWireless) _
And Not IsNull(oItem.NetConnectionStatus) _
Then WScript.Echo oItem.Name & ": " _
& aStatus(oItem.NetConnectionStatus)
Next
Reply With Quote
  #4  
Old 17-08-2009
Joey
 
Posts: n/a
how do I disable the nics showing as "Media disconnected"?

I suppose you use the method you had in mind when you wrote "I know how to
disable it with netsh ".
Reply With Quote
  #5  
Old 18-08-2009
Joey
 
Posts: n/a
Re: disable NIC based on connection state

I am trying to disable the NetConnectionID when the output shows as
DISABLED. I cant see mt oget it to work with this

iEthernet = 0: iWireless = 9
aStatus = Split("Disconnected/Connecting/Connected/Disconnecting/" _
& "Hardware not present/Hardware disabled/Hardware malfunction/" _
& "Media disconnected/Authenticating/Authentication succeeded/" _
& "Authentication failed", "/")
Set oWMIService = GetObject("winmgmts:\\.\root\CIMV2")
Set colItems = oWMIService.ExecQuery( _
"SELECT * FROM Win32_NetworkAdapter",,48)
For Each oItem In colItems
If (oItem.AdapterTypeId = iEthernet _
Or oItem.AdapterTypeId = iWireless) _
And Not IsNull(oItem.NetConnectionStatus) _
Then WScript.Echo oItem.NetConnectionID & ": " _
& aStatus(oItem.NetConnectionStatus)
If (astatus(oItem.NetConnectionStatus) = "Media disconnected")
netsh interface set interface oItem.NetConnectionID DISABLED
Next

Can you point me in the right direction?
Reply With Quote
  #6  
Old 18-08-2009
Pegasus [MVP]
 
Posts: n/a
netsh is an .exe file. You cannot invoke it directly from a VB Script. Use
the "run" or "exec" method to invoke it. If you're not familiar with these
methods, use calc.exe as a practice program before moving on to netsh. You
should also download the helpfile script56.chm from the Microsoft site.

Alternatively (and I suggest this even though you're in a scripting and not
a batch file newsgroup), you could stick to a batch file like so:
1. Use cscript.exe to invoke the script I gave you.
2. Terminate it with wscript.quite(x) where you set x=0 if the adapter is
disconnected, x=1 if it is connected.
3. Use your batch file to check the %ErrorLevel% of cscript.exe. "0" means
the adapter is disconnected.
4. Invoke netsh according to this error level.

I am not sure how to pass the "NetConnectionID" to the batch file. any idea?
Reply With Quote
  #7  
Old 19-08-2009
Pegasus [MVP]
 
Posts: n/a
Re: disable NIC based on connection state

Your question is unclear. I suggest you clarify it by posting these pieces
of info:
- The output generated by my VB Script code.
- The output generated by ipconfig.exe /all
- The input expected by netsh.exe.
Reply With Quote
  #8  
Old 19-08-2009
Joey
 
Posts: n/a
Re: disable NIC based on connection state

ok here is the vbs script

iEthernet = 0
aStatus = Split("Disconnected/Connecting/Connected/Disconnecting/" _
& "Hardware not present/Hardware disabled/Hardware malfunction/" _
& "Media disconnected/Authenticating/Authentication succeeded/" _
& "Authentication failed", "/")
Set oWMIService = GetObject("winmgmts:\\.\root\CIMV2")
Set colItems = oWMIService.ExecQuery("SELECT * FROM
Win32_NetworkAdapter",,48)
For Each oItem In colItems
If (oItem.AdapterTypeId = iEthernet) Then WScript.Echo
oItem.NetConnectionID & ": " _
& aStatus(oItem.NetConnectionStatus)
If oItem.NetConnectionStatus = 7 Then Wscript.quit(0)
If oTem.NetConnectionStatus = 2 Then Wscript.quit(1)
Next

----------------------

cmd file

@ECHO OFF
cscript disablenics.vbs
echo %errorlevel%
pause
if errorlevel==0 goto disablenics
else
goto end

:disablenics
echo Disabling Unused NICs
netsh interface set interface "Local Area Connection" DISABLED

:end
cls

-------------------------

I dont know how to disable the nic within the batch file that has a
errorlevel 1. I need the NetConnectionID to disable it. but only the vb
script has it. Does it make sense?
Reply With Quote
  #9  
Old 19-08-2009
Pegasus [MVP]
 
Posts: n/a
Re: disable NIC based on connection state

In my most recent response I asked for three pieces of information.
Unfortunately you supplied none. Sorry, I cannot offer any comments. Note
also that the name you chose for my VB Script file is misleading. You called
it "DisableNICs.vbs". It should really be something like "NICStatus.vbs".

Your batch file needs a little attention too. Instead of writing
@ECHO OFF
cscript disablenics.vbs
echo %errorlevel%
pause
if errorlevel==0 goto disablenics
else
goto end

you should write
@ECHO OFF
cscript disablenics.vbs
if %errorlevel%==0 (
goto disablenics
) else (
goto end
)

or perhaps a little simpler:
@ECHO OFF
cscript //nologo disablenics.vbs || goto :eof
echo Disabling Unused NICs
netsh interface set interface "Local Area Connection" DISABLED

Using %errorlevel% on two separate lines of code is risky - it might get
changed by the first line!
Reply With Quote
  #10  
Old 19-08-2009
Joey
 
Posts: n/a
Re: disable NIC based on connection state

Thank for helping me out on this.

- The output generated by my VB Script code.

C:\software>cscript disablenics.vbs
Microsoft (R) Windows Script Host Version 5.6
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.

Local Area Connection: Media disconnected
Local Area Connection 2: Media disconnected
Local Area Connection 3: Connected
Local Area Connection 4: Media disconnected

- The output generated by ipconfig.exe /all

C:\software>ipconfig /all

Windows IP Configuration

Host Name . . . . . . . . . . . . : host
Primary Dns Suffix . . . . . . . : dom.com
Node Type . . . . . . . . . . . . : Hybrid
IP Routing Enabled. . . . . . . . : No
WINS Proxy Enabled. . . . . . . . : No
DNS Suffix Search List. . . . . . : dom.com

Ethernet adapter Local Area Connection 3:

Connection-specific DNS Suffix . : dom.com
Description . . . . . . . . . . . : Broadcom BCM5708C NetXtreme II GigE
(NDIS
VBD Client) #3
Physical Address. . . . . . . . . : 00-24-E8-6B-9A-2E
DHCP Enabled. . . . . . . . . . . : Yes
Autoconfiguration Enabled . . . . : Yes
IP Address. . . . . . . . . . . . : 10.50.233.12
Subnet Mask . . . . . . . . . . . : 255.255.0.0
Default Gateway . . . . . . . . . : 10.50.0.5
DHCP Server . . . . . . . . . . . : 10.50.1.201
DNS Servers . . . . . . . . . . . : 10.50.1.56
10.50.1.42
Primary WINS Server . . . . . . . : 10.50.1.201
Lease Obtained. . . . . . . . . . : Monday, August 17, 2009 5:08:03 PM
Lease Expires . . . . . . . . . . : Tuesday, August 25, 2009 5:08:03 PM

Ethernet adapter Local Area Connection 4:

Media State . . . . . . . . . . . : Media disconnected
Description . . . . . . . . . . . : Broadcom BCM5708C NetXtreme II GigE
(NDIS
VBD Client) #4
Physical Address. . . . . . . . . : 00-24-E8-6B-9A-30

Ethernet adapter Local Area Connection 2:

Media State . . . . . . . . . . . : Media disconnected
Description . . . . . . . . . . . : Broadcom BCM5708C NetXtreme II GigE
(NDIS
VBD Client) #2
Physical Address. . . . . . . . . : 00-24-E8-6B-9A-34

Ethernet adapter Local Area Connection:

Media State . . . . . . . . . . . : Media disconnected
Description . . . . . . . . . . . : Broadcom BCM5708C NetXtreme II GigE
(NDIS
VBD Client)
Physical Address. . . . . . . . . : 00-24-E8-6B-9A-32

C:\software>

- The input expected by netsh.exe.
I need to pass the NetconnectionID to netsh. which is Local Area Connection,
Local Area Connection 2, etc...
Reply With Quote
  #11  
Old 19-08-2009
Joey
 
Posts: n/a
Re: disable NIC based on connection state

iEthernet = 0
aStatus = Split("Disconnected/Connecting/Connected/Disconnecting/" _
& "Hardware not present/Hardware disabled/Hardware malfunction/" _
& "Media disconnected/Authenticating/Authentication succeeded/" _
& "Authentication failed", "/")
Set oWMIService = GetObject("winmgmts:\\.\root\CIMV2")
Set colItems = oWMIService.ExecQuery("SELECT * FROM
Win32_NetworkAdapter",,48)
For Each oItem In colItems
If (oItem.AdapterTypeId = iEthernet) Then WScript.Echo
oItem.NetConnectionID & ": " _
& aStatus(oItem.NetConnectionStatus)
If oItem.NetConnectionStatus = 7 Then Wscript.quit(0)
If oItem.NetConnectionStatus = 2 Then Wscript.quit(1)
Next

See that I am using NetConnectionStatus? It quits right after it sees the
first disconnected NIC. how can I have it go through all NICs before it
quit?
Reply With Quote
  #12  
Old 20-08-2009
Pegasus [MVP]
 
Posts: n/a
Re: disable NIC based on connection state

Don't use wscript.quit - store the status in a variable instead so that you
can continue with your loop. And get rid of the aStatus array - you never
use it in your code!
Reply With Quote
  #13  
Old 20-08-2009
Pegasus [MVP]
 
Posts: n/a
Re: disable NIC based on connection state

I know very little about netsh.exe. Try this script instead:
[01] '-------------------------
[02] 'Disable disconnected NICs
[03] 'Prerequisite: devcon.exe
[04] '19.8.2009 FNL
[05] '-------------------------
[06] sDevcon = "d:\Tools\devcon.exe"
[07] iEthernet = 0: iWireless = 9
[08] Set oWshShell = CreateObject("WScript.Shell")
[09]
[10] '-------------------------
[11] 'Get a list of all devices
[12] '-------------------------
[13] WScript.Echo "Compiling the device list"
[14] Set oExec = oWshShell.Exec(sDevcon & " HWIDs *")
[15] WScript.Sleep 5000
[16] sResult = ""
[17] While Not oExec.StdOut.AtEndOfStream
[18] sResult = sResult & oExec.StdOut.ReadLine & "|"
[19] Wend
[20] aDeviceInfo = Split(sResult, "|")
[21]
[22] '---------------------------------
[23] 'Check which adapters are disabled
[24] '---------------------------------
[25] Set oWMIService = GetObject("winmgmts:\\.\root\CIMV2")
[26] Set colItems = oWMIService.ExecQuery(_
[27] "SELECT * FROM Win32_NetworkAdapter", , 48)
[28] For Each oItem In colItems
[29] If (oItem.AdapterTypeId = iEthernet _
[30] Or oItem.AdapterTypeId = iWireless) _
[31] And oItem.NetConnectionStatus = 7 _
[32] Then DisableAdapter(oItem.Name)
[33] Next
[34]
[35] '-----------------------------
[36] 'Disable disconnected adapters
[37] '-----------------------------
[38] Sub DisableAdapter (sName)
[39] For i = 0 To UBound(aDeviceInfo) - 1
[40] If LTrim(aDeviceInfo(i)) = "Name: " & sName Then
[41] WScript.Echo "Disabling", sName
[42] Set oExec = oWshShell.Exec(sDevcon _
[43] & " disable " & aDeviceInfo(i+2))
[44] Do
[45] WScript.Sleep 200
[46] Loop Until oExec.Status = 1
[47] End If
[48] Next
[49] End Sub
Reply With Quote
  #14  
Old 20-08-2009
acomputerwiz6
 
Posts: n/a
Try this

iEthernet = 0
Set oWMIService = GetObject("winmgmts:\\.\root\CIMV2")
Set oShell = CreateObject("WScript.Shell")
Set colItems = oWMIService.ExecQuery("SELECT * FROM
Win32_NetworkAdapter",,48)
For Each oItem In colItems
If (oItem.AdapterTypeId = iEthernet) Then
sNetID = oItem.NetConnectionID
If oItem.NetConnectionStatus = 7 Then
oShell.run "cmd /c netsh interface set interface " & Chr(34) &
sNetID & Chr(34) & " DISABLED",0,TRUE
End If
End If
Next

the oShell.run should all be on one line...
Reply With Quote
  #15  
Old 16-09-2009
Joey
 
Posts: n/a
Re: disable NIC based on connection state

wow this is nice. thanks!
Reply With Quote
Reply

  TechArena Community > Technical Support > Computer Help > Windows Server > Windows Server Help


Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "disable NIC based on connection state"
Thread Thread Starter Forum Replies Last Post
Is there any way to clear CLOSE_WAIT state of a TCP connection Vj@y.Deenanath Networking & Security 6 30-12-2011 05:31 AM
iPad disconnects from digital certificate based SSL connection Petru123 Portable Devices 5 11-02-2011 11:46 PM
OCZ Announces Vertex EX SLC-based Solid State Drives deoWo Hardware Peripherals 3 21-04-2009 08:54 AM
Bluetooth mouse looses connection after Vista sleep state roger Vista Hardware Devices 14 19-01-2009 09:42 PM
Linux based connection sharing Michael25 Operating Systems 1 21-10-2008 02:15 PM


All times are GMT +5.5. The time now is 11:01 PM.