Discussion:
disable NIC based on connection state
(too old to reply)
Joey
2009-08-12 23:41:26 UTC
Permalink
Is there a script that will disable a nic based on the connection state?
Pegasus [MVP]
2009-08-13 05:18:31 UTC
Permalink
Post by Joey
Is there a script that will disable a nic based on the connection state?
Please elaborate.
Joey
2009-08-14 20:49:56 UTC
Permalink
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
Post by Pegasus [MVP]
Post by Joey
Is there a script that will disable a nic based on the connection state?
Please elaborate.
Pegasus [MVP]
2009-08-14 22:04:41 UTC
Permalink
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
Post by Joey
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
Post by Pegasus [MVP]
Post by Joey
Is there a script that will disable a nic based on the connection state?
Please elaborate.
Joey
2009-08-16 23:46:47 UTC
Permalink
how do I disable the nics showing as "Media disconnected"?
Post by Pegasus [MVP]
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
Post by Joey
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
Post by Pegasus [MVP]
Post by Joey
Is there a script that will disable a nic based on the connection state?
Please elaborate.
Pegasus [MVP]
2009-08-17 05:38:58 UTC
Permalink
I suppose you use the method you had in mind when you wrote "I know how to
disable it with netsh ".
Post by Joey
how do I disable the nics showing as "Media disconnected"?
Joey
2009-08-17 17:37:18 UTC
Permalink
Hi

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?

Thanks
Post by Pegasus [MVP]
I suppose you use the method you had in mind when you wrote "I know how to
disable it with netsh ".
Post by Joey
how do I disable the nics showing as "Media disconnected"?
Pegasus [MVP]
2009-08-17 18:03:43 UTC
Permalink
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.
Post by Joey
Hi
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?
Thanks
Post by Pegasus [MVP]
I suppose you use the method you had in mind when you wrote "I know how to
disable it with netsh ".
Post by Joey
how do I disable the nics showing as "Media disconnected"?
Joey
2009-08-18 21:03:23 UTC
Permalink
I am not sure how to pass the "NetConnectionID" to the batch file. any idea?

Thanks
Post by Pegasus [MVP]
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
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.
Post by Joey
Hi
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?
Thanks
Post by Pegasus [MVP]
I suppose you use the method you had in mind when you wrote "I know how
to disable it with netsh ".
Post by Joey
how do I disable the nics showing as "Media disconnected"?
Pegasus [MVP]
2009-08-18 21:49:49 UTC
Permalink
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.
Post by Joey
I am not sure how to pass the "NetConnectionID" to the batch file. any idea?
Thanks
Joey
2009-08-18 22:07:47 UTC
Permalink
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?
Post by Pegasus [MVP]
Your question is unclear. I suggest you clarify it by posting these pieces
- The output generated by my VB Script code.
- The output generated by ipconfig.exe /all
- The input expected by netsh.exe.
Post by Joey
I am not sure how to pass the "NetConnectionID" to the batch file. any idea?
Thanks
Pegasus [MVP]
2009-08-18 22:32:55 UTC
Permalink
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!
Post by Joey
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?
Post by Pegasus [MVP]
Your question is unclear. I suggest you clarify it by posting these
- The output generated by my VB Script code.
- The output generated by ipconfig.exe /all
- The input expected by netsh.exe.
Post by Joey
I am not sure how to pass the "NetConnectionID" to the batch file. any idea?
Thanks
Joey
2009-08-18 23:04:50 UTC
Permalink
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...
Post by Pegasus [MVP]
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
)
@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!
Post by Joey
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?
Post by Pegasus [MVP]
Your question is unclear. I suggest you clarify it by posting these
- The output generated by my VB Script code.
- The output generated by ipconfig.exe /all
- The input expected by netsh.exe.
Post by Joey
I am not sure how to pass the "NetConnectionID" to the batch file. any idea?
Thanks
Pegasus [MVP]
2009-08-19 20:32:12 UTC
Permalink
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
Post by Joey
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
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
Media State . . . . . . . . . . . : Media disconnected
Description . . . . . . . . . . . : Broadcom BCM5708C NetXtreme II GigE
(NDIS
VBD Client) #4
Physical Address. . . . . . . . . : 00-24-E8-6B-9A-30
Media State . . . . . . . . . . . : Media disconnected
Description . . . . . . . . . . . : Broadcom BCM5708C NetXtreme II GigE
(NDIS
VBD Client) #2
Physical Address. . . . . . . . . : 00-24-E8-6B-9A-34
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...
Joey
2009-08-18 23:12:27 UTC
Permalink
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?
Post by Pegasus [MVP]
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
)
@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!
Post by Joey
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?
Post by Pegasus [MVP]
Your question is unclear. I suggest you clarify it by posting these
- The output generated by my VB Script code.
- The output generated by ipconfig.exe /all
- The input expected by netsh.exe.
Post by Joey
I am not sure how to pass the "NetConnectionID" to the batch file. any idea?
Thanks
Pegasus [MVP]
2009-08-19 20:32:07 UTC
Permalink
Post by Pegasus [MVP]
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?
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!
acomputerwiz6
2009-08-20 13:14:35 UTC
Permalink
Post by Pegasus [MVP]
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?
Post by Pegasus [MVP]
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
)
@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!
Post by Joey
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?
Post by Pegasus [MVP]
Your question is unclear. I suggest you clarify it by posting these
- The output generated by my VB Script code.
- The output generated by ipconfig.exe /all
- The input expected by netsh.exe.
Post by Joey
I am not sure how to pass the "NetConnectionID" to the batch file. any idea?
Thanks
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
acomputerwiz6
2009-08-20 13:16:31 UTC
Permalink
Post by Pegasus [MVP]
Post by Pegasus [MVP]
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?
Post by Pegasus [MVP]
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
)
@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!
Post by Joey
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?
Post by Pegasus [MVP]
Your question is unclear. I suggest you clarify it by posting these
- The output generated by my VB Script code.
- The output generated by ipconfig.exe /all
- The input expected by netsh.exe.
Post by Joey
I am not sure how to pass the "NetConnectionID" to the batch file. any idea?
Thanks
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...
Joey
2009-09-16 00:31:40 UTC
Permalink
wow this is nice. thanks!
Post by Pegasus [MVP]
Post by Pegasus [MVP]
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?
Post by Pegasus [MVP]
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
)
@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!
Post by Joey
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?
Post by Pegasus [MVP]
Your question is unclear. I suggest you clarify it by posting these
- The output generated by my VB Script code.
- The output generated by ipconfig.exe /all
- The input expected by netsh.exe.
Post by Joey
I am not sure how to pass the "NetConnectionID" to the batch file.
any
idea?
Thanks
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...

Loading...