Toggle NIC on/off with vbs - XP and 7

Discussion in 'other software & services' started by Sully, Feb 20, 2010.

Thread Status:
Not open for further replies.
  1. Sully

    Sully Registered Member

    Joined:
    Dec 23, 2005
    Posts:
    3,719
    I used this script in XP for a number of years. Simply edit the script for the network connection you wish to toggle and double click.

    The XP version:
    Code:
    '~ Toggle a SPECIFIED NIC on or off
    Option Explicit
    Dim objShell
    Dim objCP, objEnable, objDisable, colNetwork
    Dim clsConn, clsLANConn, clsVerb
    Dim strNetConn, strConn, strEnable, strDisable
    Dim bEnabled, bDisabled
    
    strNetConn = "Network Connections"
    
    ' edit this variable to match your chosen NIC to toggle
    strConn = "Local Area Connection"
    
    strEnable = "En&able"
    strDisable = "Disa&ble"
    
    Set objShell = CreateObject("Shell.Application")
    Set objCP = objShell.Namespace(3) 'Control Panel
    
    Set colNetwork = Nothing
    For Each clsConn in objCP.Items
    	If clsConn.Name = strNetConn Then
    		Set colNetwork = clsConn.getfolder
    		Exit For
    	End If
    Next
    
    If colNetwork is Nothing Then
    	WScript.Echo "Network folder not found"
    	WScript.Quit
    End If
    
    Set clsLANConn = Nothing
    For Each clsConn in colNetwork.Items
    '~ ‘In case the LAN is named "connection 2", etc.
    
    	If Instr(LCase(clsConn.name),LCase(strConn)) Then
    		Set clsLANConn = clsConn
    		Exit For
    	End If
    Next
    
    If clsLANConn is Nothing Then
    	WScript.Echo "Network Connection not found"
    	WScript.Quit
    End If
    
    bEnabled = True
    Set objEnable = Nothing
    Set objDisable = Nothing
    For Each clsVerb in clsLANConn.verbs
    	If clsVerb.name = strEnable Then 
    		Set objEnable = clsVerb 
    		bEnabled = False
    	End If
    	If clsVerb.name = strDisable Then 
    		Set objDisable = clsVerb 
    	End If
    Next
    
    If bEnabled Then
    	objDisable.DoIt
    Else
    	objEnable.DoIt
    End If
    
    '~ Give the connection time to stop/start
    WScript.Sleep 1000 
    WScript.Quit
    The Win 7 version:
    Code:
    
    '~ Toggle a SPECIFIED NIC on or off
    Option Explicit
    
    Const NETWORK_CONNECTIONS = &H31&
    
    Dim objShell, objFolder, objFolderItem, objEnable, objDisable
    Dim folder_Object, target_NIC
    Dim NIC, clsVerb
    Dim str_NIC_Name, strEnable, strDisable
    Dim bEnabled, bDisabled
    
    ' ========================================================
    ' ===== place the name of your network adapter here ======
    ' examples:
    ' str_NIC_Name = "Local Area Connection 2"
    ' str_NIC_Name = "Wireless Connection 1"
    ' ========================================================
    str_NIC_Name = "Local Area Connection"
    ' ========================================================
    
    strEnable = "En&able"
    strDisable = "Disa&ble"
    
    ' create objects and get items
    Set objShell = CreateObject("Shell.Application")
    Set objFolder = objShell.Namespace(NETWORK_CONNECTIONS)
    Set objFolderItem = objFolder.Self
    Set folder_Object = objFolderItem.GetFolder
    
    ' see if the namespace exists
    If folder_Object Is Nothing Then
    	Wscript.Echo "Could not find Network Connections"
    	WScript.Quit
    End If
    
    Set target_NIC = Nothing
    
    ' look at each NIC and match to the chosen name
    For Each NIC In folder_Object.Items
    	If LCase(NIC.Name) = LCase(str_NIC_Name) Then
    		' proper NIC is found, get it
    		Set target_NIC = NIC
    	End If
    Next
    
    If target_NIC Is Nothing Then
    	WScript.Echo "Unable to locate proper NIC"
    	WScript.Quit
    End If
    
    bEnabled = True
    Set objEnable = Nothing
    Set objDisable = Nothing
    
    For Each clsVerb In target_NIC.Verbs
    	'~ Wscript.Echo clsVerb
    	If clsVerb.Name = strEnable Then
    		Set objEnable = clsVerb
    		bEnabled = False
    	End If
    	If clsVerb.Name = strDisable Then
    		Set objDisable = clsVerb
    	End If
    Next
    
    If bEnabled Then
    	objDisable.DoIt
    Else
    	objEnable.DoIt
    End If
    
    '~ Give the connection time to stop/start
    WScript.Sleep 1000
    WScript.Quit
    
    For those unfamiliar with vbs scripts, just create a text file, rename to <some name>.vbs

    Sul.
     
  2. Huupi

    Huupi Registered Member

    Joined:
    Sep 2, 2006
    Posts:
    2,024
    I just rightclick the tray icon and choose to disable it.
     
  3. Sully

    Sully Registered Member

    Joined:
    Dec 23, 2005
    Posts:
    3,719
    Really? How would you do that in win 7? In xp, yes indeed that is how I usually did it.

    Adapt that script for other uses though.

    For example, if you were to attend a lan party with no internet (or similiar situation) and you start a game (such as a CoD game), it might sit still, for a certain specified time, "looking" for internet, until finally finishing loading.

    Take this script, in whatever flavor you like, with a timer maybe, and execute it. Now you can disable the NIC, the game loads very fast because with a disabled NIC it does not bother to see if it can get online, then after a bit the script runs again and toggles the NIC back on. And no Alt-Tab involved, only some automation.

    Or you can hotkey it.

    Or suppose you made a menu that is customizable, like a tray menu, with all the things you normally need. Programs, tools, etc. Maybe you could get that menu, very much like a context menu, to come to wherever your mouse is with a hotkey. So it would not matter what window had focus (except fullscreen 3d) wherever the mouse is, a menu appear at it. Now you could put an option on that menu to, disable the network. Or maybe change the gateway. Or the IP.

    Suppose you are about to try some new software, or test a virus file, or some other situation where you want to kill the ability for anything to get out. You could make a firewall rule I suppose. You could go to the tray and right click, choose disable. You could also just run a script on the desktop. Or maybe your menu has these things built into it.

    Maybe netsh is more the answer. Or coding some sendkey script.

    You get the point. Things like this particular one you can find, sure, but maybe someone else can use it to do different things. It is not so easy to just create a batch file or a run command to achieve this. I wish it were.

    I just like automation because then I am not limited to doing it manually. Nerd mentality I guess ;)

    Sul.
     
  4. Huupi

    Huupi Registered Member

    Joined:
    Sep 2, 2006
    Posts:
    2,024
    Just XP here so hence my reaction ,i dont bother with scripts if i can do it manually in two clicks .

    For photoshop and backup stuff i have a bunch of scripts running but then with these applications its a big timesaver if you need to process hundreds of files at once.
     
Loading...
Thread Status:
Not open for further replies.