I have been searching around the web for some time looking for a batch script that can help me switch between static and dhcp. I came across this script on Stackoverflow that also gives the user an option to choose the Connected NIC that needed to be changed. Really liked this option since some users change the name on there ethernet card for some reason.
https://stackoverflow.com/questions/219 ... figuration
Code: Select all
@echo off
setlocal enabledelayedexpansion
set cnt=0
for /f "tokens=1,4,5*" %%a in (
'Netsh Int IPv4 show interfaces^| findstr /i /r /v "dis"^| more +3'
) do (
set /a cnt+=1
set "idx.!cnt!=%%a"
set "idx.!cnt!.name=%%c %%d"
)
:Configure
echo.
echo Choose which adapter to configure.
for /l %%a in (1,1,!cnt!) do (
if !idx.%%a.configured!==1 (
echo !idx.%%a! - !idx.%%a.name! - Configured
) ELSE (
echo !idx.%%a! - !idx.%%a.name!
)
)
echo.
set /p "idx=Enter index to configure: "
for %%a in (%idx%) do (
echo.
echo.
echo ======================
echo DHCP or static?
echo press 1 for DHCP, 2 to configure manually.
echo ======================
echo.
echo.
set /p "choice=Choose option 1 or 2: "
if '!choice!'=='1' (
echo You chose automatic settings for !idx.%%a.name!
echo netsh int ipv4 set address %%a dhcp
echo netsh int ipv4 dnsservers %%a dhcp
set "idx.%%a.configured=1"
) ELSE (
if '!choice!'=='2' (
echo Chosen for static settings.
set /p varip=Enter the ip adress:
set /p varsm=Enter the subnetmask:
set /p vargw=Enter the gateway:
set /p vardns1=Enter DNS Server:
echo netsh int ipv4 set address %%a static !varip! !varsm! !vargw! 1
echo netsh int ipv4 set dnsservers %%a static !vardns1! primary
set "idx.%%a.configured=1"
)
)
)
set /p doagain="Would you like to configure another adapter? (Y/N):
if /i '%doagain%'=='Y' (goto :configure) else Echo Configuration complete.
I have been trying to understand the 'for'-loop, but im in way over my head.
What im looking for is the option to use !idx.%%a! as a option to select the !idx.%%a.name! (if this even is possible). But %%a only holds the information typed in by the user, so if you have a typo the script will not work.
Im therfor hopeing there are some cleaver minds here that can help me out?
Thanks.