Tech Support Forum banner
Status
Not open for further replies.

batch file/script help!

5.4K views 9 replies 4 participants last post by  TheOutcaste  
#1 ·
Me and my friends are constantly plagued by Local Area Network connection issues when trying to connect to each other for streaming, game, etc. I was wondering how to create a file (like a batch file or script) that would execute the following commands to make the process quicker and easier to set computers up for a LAN.

- disable windows firewall
- disable/enable network adapters
- end unneeded processes such as ituneshelper.exe, ApplemMobileDeviceService.exe, etc.
- set computer's IP address to a specific static one (i.e. 192.168.1.10) as well as dns servers to particular settings

The whole point is to ease the process of connecting several computers on short notice in a short amount of time. I just need help learning how to write such a script or batch file or something that can execute all these things.

Thank you in advance for your help
 
#4 ·
Note that you can't disable network adapters in XP from the command line or in a WSH Script. You can in Vista.
You can use devcon.exe in XP to disable devices.

In a batch file you can use Netsh, or better yet WMIC to configure the network adapters and firewall. TaskKill (XP Pro/Vista) or tskill (XP Home) can be used to end processes, SC to start/stop services.

Or a WSH Script/Jscript/VBScript as wulfgarpro has suggested
 
Save
#7 · (Edited by Moderator)
Ok, so i made a batch file. But i need some help, I'm horrible at coding and i can't figure out why my syntax is off when trying to enable the Local Area Connection. Also if anyone can think of any other process that i should kill to help facilitate a clean connection please let me know.

Here is the code:


Code:
:START
echo off
cls
SETLOCAL
netsh interface set interface "Local Area Connection" enabled
netsh firewall set opmode mode=disable
netsh int ip set address name="Local Area Connection" source=static addr=192.168.1.212 mask=255.255.255.0
netsh int ip set address name="Local Area Connection" source=static gateway=192.168.1.1 gwmetric=1
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

:MENU
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.
SET /P M=Type 1, 2, or 3 then press ENTER:
IF %M%==1 GOTO RESTART
IF %M%==3 GOTO EXIT
IF %M%==2 GOTO DYNAMIC
:RESTART
GOTO START
:EXIT
ENDLOCAL
exit
:DYNAMIC
netsh interface ip set address "Local Area Connection" dhcp
GOTO MENU2

:MENU2
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.
SET /P M=Type 1, 2, or 3 then press ENTER:
IF %M%==1 GOTO RESTART
IF %M%==3 GOTO EXIT
IF %M%==2 GOTO START
:RESTART
GOTO START
:EXIT
ENDLOCAL
exit


It is really elementary, I know, so any pointers are appreciated.
 
#8 ·
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
 
Save
#9 ·
I was able to give your changes a test. Everything worked beautifully. There was an equal symbol missing between _IPAdr and 192.168.1.212 on line 5, but easy enough fix.

Thanks so much for the vbscript, it worked like a charm! I definitely need to learn how to write in visual basic.

IF anyone can think of any more additions to the script or tasks to kill to help create a lag-free connection that would be wonderful.

Thanks Again!
 
#10 ·
Glad it's working for you.
Sorry about the typo, it's usually the quotes that get me. They must have subcontracted the job of messing with me to the equal sign:eek:
 
Save
Status
Not open for further replies.
You have insufficient privileges to reply here.