I knew you couldn't disable an adapter, but I thought you cound disable the connection, but apparently you can't disable/enable a LAN connection using Netsh on XP:
Code:
C:\>netsh int set int /?
Usage set interface [name = ] IfName
[ [admin = ] ENABLED|DISABLED
[connect = ] CONNECTED|DISCONNECTED
[newname = ] NewName ]
Sets interface parameters.
IfName - the name of the interface
admin - whether the interface should be enabled (non-LAN only).
connect - whether to connect the interface (non-LAN only).
newname - new name for the interface (LAN only).
Notes:
- At least one option other than the name must be specified.
- If connect = CONNECTED is specified, then the interface
is automatically enabled even if the admin = DISABLED
option is specified.
It does work on Server 2003, and odd thing is if you disable the Local Area Connection from the Network Connections folder,
netsh int show int will still show it as enabled.
I did find a WSH Script to toggle a Network Connection, and modified it so you can enable or disable a specific connection by passing the name and desired action.
I modified your batch to use that file, and also changed it to use variables. This way you just need to change the IP and Network Connection name at one spot at the start of the file rather than searching through the file to find all the locations. Also set the Menus to check for a valid entry, put the Setlocal before the Start label so it doesn't get executed multiple times, removed duplicate labels and unneeded labels.
Here's the modified batch file:
Code:
@Echo Off
SetLocal
Set _ConName=Local Area Connection
Set _ScriptName=SetNetCon.vbs
Set _IPAdr 192.168.1.212
Set _Sub=255.255.255.0
Set _Gateway=192.168.1.1
Set _Metric=1
:_Start
Cls
CScript //nologo "%~dp0%_ScriptName%" "%_ConName%" Enable
Netsh firewall Set opmode mode=disable
Netsh int ip Set address name="%_ConName%" source=static addr=%_IPAdr% mask=%_Sub%
Netsh int ip Set address name="%_ConName%" source=static gateway=%_Gateway% gwmetric=%_Metric%
TaskKill /F /IM ipod.exe /IM AppleMobileDeviceService.exe /IM ituneshelper.exe /IM WINWORD.EXE /IM iPodService.exe /IM uTorrent.exe /IM bitlord.exe /IM bittorrent.exe /IM limewire.exe /IM McAgent.exe /IM Mcods.exe /IM McNaSvc.exe /IM McUpdmgr.exe /IM McLogsrv.exe /IM Mpfsrv.exe /IM mcusrmgr.exe /IM mctskshd.exe /IM mcshield.exe /IM redirsvc.exe /IM mcpromgr.exe /IM mcdetect.exe /IM Steam.exe
Cls
Echo.
Echo ........................................................
Echo PRESS 1 or 2 then enter to select your task
Echo ........................................................
Echo.
Echo -------------------------------------------------------------
Echo 1 - Restart Optimizer
Echo -------------------------------------------------------------
Echo 2 - Switch back to dynamic IP
Echo -------------------------------------------------------------
Echo 3 - Exit
Echo.
:_Ask1
Set /P M=Type 1, 2, or 3 then press ENTER:
If %M%==1 Goto _Start
If %M%==3 Goto _Exit
If Not %M%==2 Echo Enter 1, 2, or 3 only&Goto _Ask1
Netsh interface ip Set address " %_ConName%" dhcp
Cls
Echo.
Echo ........................................................
Echo YOU NOW HAVE A DYNAMIC IP ADDRESS - SELECT A TASK
Echo ........................................................
Echo.
Echo -------------------------------------------------------------
Echo 1 - Restart Optimizer
Echo -------------------------------------------------------------
Echo 2 - Switch back to static IP
Echo -------------------------------------------------------------
Echo 3 - Exit
Echo.
:_Ask2
Set /P M=Type 1, 2, or 3 then press ENTER:
If %M%==1 Goto _Start
If %M%==3 Goto _Exit
If %M%==2 Goto _Start
Echo Enter 1, 2, or 3 only&Goto _Ask2
:_Exit
EndLocal
Exit
Here's the script. Must be in the same folder as the batch file, you can name it what you want, just change the _ScriptName variable in the batch file to match:
Code:
' strConnectionName = "Local Area Connection"
' Pass Connection name and desired action, enable or disable
strConnectionName = WScript.Arguments(0)
strAction = LCase(WScript.Arguments(1))
strEnableVerb = "En&able"
strDisableVerb = "Disa&ble"
Set shellApp = CreateObject("Shell.Application")
Set objControlPanel = ShellApp.Namespace(3)
Set objNetConnections = nothing
For each FolderItem in objControlPanel.Items
If FolderItem.Name = "Network Connections" Then
Set objNetConnections = FolderItem.GetFolder: Exit For
End If
Next
If objNetConnections is nothing Then
MsgBox "Couldn't find 'Network Connections' folder"
WScript.Quit
End If
Set objLanConnection = nothing
For each FolderItem in objNetConnections.Items
If LCase(FolderItem.Name) = LCase(strConnectionName) Then
Set objLanConnection = FolderItem: Exit For
End If
Next
If objLanConnection is nothing Then
MsgBox "Couldn't find '" & strConnectionName & "' item"
WScript.Quit
End If
boolEnabled = true
Set objEnableVerb = nothing
Set objDisableVerb = nothing
s = "Verbs: " & vbcrlf
For each verb in objLanConnection.verbs
s = s & vbcrlf & verb.Name
If verb.Name = strEnableVerb Then
Set objEnableVerb = verb
boolEnabled = false
End If
If verb.Name = strDisableVerb Then
Set objDisableVerb = verb
End If
Next
'debugging displays left just in case...
'
'MsgBox s ': WScript.Quit
'MsgBox "Enabled: " & boolEnabled ': WScript.Quit
'not sure why, but invokeverb always seemed to work
'For enable but not disable.
'
'saving a reference to the appropriate verb object
'and calling the DoIt method always seems to work.
'
If boolEnabled Then
If strAction = "disable" Then
objDisableVerb.DoIt
End If
Else
If strAction = "enable" Then
objEnableVerb.DoIt
End If
End If
'adjust the sleep duration below as needed...
'
'If you let the objLanConnection go out of scope
'and be destroyed too soon, the action of the verb
'may not take...
'
WScript.Sleep 1000
Give it a try and see how it works