disable NIC based on connection state

Discussion in 'Scripting' started by Joey, Aug 13, 2009.

  1. Joey

    Joey Guest

    Is there a script that will disable a nic based on the connection state?
     
    Joey, Aug 13, 2009
    #1
    1. Advertisements

  2. Please elaborate.
     
    Pegasus [MVP], Aug 13, 2009
    #2
    1. Advertisements

  3. Joey

    Joey Guest

    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
     
    Joey, Aug 14, 2009
    #3
  4. 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
     
    Pegasus [MVP], Aug 14, 2009
    #4
  5. Joey

    Joey Guest

    how do I disable the nics showing as "Media disconnected"?
     
    Joey, Aug 17, 2009
    #5
  6. I suppose you use the method you had in mind when you wrote "I know how to
    disable it with netsh ".
     
    Pegasus [MVP], Aug 17, 2009
    #6
  7. Joey

    Joey Guest

    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
     
    Joey, Aug 17, 2009
    #7
  8. 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.
     
    Pegasus [MVP], Aug 17, 2009
    #8
  9. Joey

    Joey Guest

    I am not sure how to pass the "NetConnectionID" to the batch file. any idea?

    Thanks
     
    Joey, Aug 18, 2009
    #9
  10. 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.
     
    Pegasus [MVP], Aug 18, 2009
    #10
  11. Joey

    Joey Guest

    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?
     
    Joey, Aug 18, 2009
    #11
  12. 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!
     
    Pegasus [MVP], Aug 18, 2009
    #12
  13. Joey

    Joey Guest

    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...
     
    Joey, Aug 19, 2009
    #13
  14. Joey

    Joey Guest

    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?
     
    Joey, Aug 19, 2009
    #14
  15. 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!
     
    Pegasus [MVP], Aug 19, 2009
    #15
  16. 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
     
    Pegasus [MVP], Aug 19, 2009
    #16
  17. Joey

    Joey Guest

    wow this is nice. thanks!
    the oShell.run should all be on one line...
     
    Joey, Sep 16, 2009
    #17
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.