Wonko,
I was really longing for your code contribution here.
As always, a "nice to have" one.
For me your code only works after adding the open and closed bracket after "DO".
I also felt free to add some line spacing and the missing action.
As for Wonko - just a working example batch (ALL credits to Wonko):
@ECHO OFF
SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
SET Status0=Disabled
SET Status2=Enabled
SET Action0=enable
SET Action2=disable
FOR /F "tokens=2,3,4 delims=," %%A IN ('WMIC path win32_NetworkAdapter Get Index^, Name^, NetConnectionStatus^, PNPDeviceID /format:csv ^| FIND "PCI\VEN"') DO CALL :PhysNIC %%A "%%B" %%C
GOTO :EOF
:PhysNIC
SET Index=%1
ECHO Physical Nic found:
ECHO Index=%1 : %~2
SET Status=!Status%3!&SET Action=!Action%3!
CALL :%Status% %%A
GOTO :EOF
:Enabled
WMIC path win32_NetworkAdapterConfiguration where "Index = %Index%" Get IPAddress /format
:Disabled
IF %Status%==%Status0% ECHO.
ECHO Device is currently %Status%
SET /P "Choice= Press enter to %Action% it."
IF "%Choice%"=="" wmic path win32_networkadapter where index=%Index% call %Action%
GOTO :EOF
In the meanwhile I tried to rewrite the netsh approach.
Through the localisation issues I decided to display all adapters with a menu numbering.
In this way the user is able to choose the adapter of his choice - a more accurate decision is possible then.
Here's the example code (works similar to the first one I posted):
@ECHO OFF
SETLOCAL ENABLEEXTENSIONS
:: if first commandline parameter is '-e' then enable adapter. Otherwise disable adapter.
IF /I "%~1"=="-e" (SET _Action=ENABLED& SET "_MsgStr=enable"
) ELSE (SET _Action=DISABLED& SET _MsgStr=disable)
:: check for netsh.exe
DIR /B "%SYSTEMROOT%\System32\netsh.exe" >NUL 2>&1||ECHO.&& ECHO netsh.exe not found ...exit script&& GOTO:_END
:: get, count-up and store all network adapters by Name [_NICn] - tested in XP, 7 and 10
:: this loop also works for vmware virtual machines [tests in XP and Seven] were column 'Status' may be empty
FOR /F "skip=3 tokens=3* delims= " %%a IN ('netsh interface show interface') DO (
SET /A "_CountA+=1" & IF "%%b"=="" (CALL SET _NIC%%_COUNTA%%=%%a) ELSE (CALL SET _NIC%%_COUNTA%%=%%b))
SET _NIC >NUL 2>&1||ECHO No network adapters found ...exit script&& GOTO:_End
:_Menu
:: display netsh interface screen with menu numbers and get max menu count
CLS
SET "_CountB=& SET _CountMAX=& SET _Input=& SET _CHOICE="
ECHO.& FOR /F "skip=3 tokens=* delims= " %%a IN ('netsh interface show interface') DO (
SET /A _CountB+=1& CALL ECHO %%_COUNTB%%: %%a & CALL SET "_CountMax=%%_COUNTB%%")
:: Choice dialog
ECHO.& SET /P "_INPUT=Please select a network adapter by menu number."
:: validate user input [_CHOICE variable should be defined after validation]
CALL :_Validate _INPUT _CountMAX _CHOICE
:: re-display menu if _Choice variable is not defined
IF NOT DEFINED _CHOICE PAUSE>NUL&GOTO:_Menu
:: if _Choice matches stored _NIC name enable/disable adapter
FOR /F "tokens=1* delims==" %%a IN ('SET _NIC') DO (
IF "_NIC%_CHOICE%" EQU "%%a" (
SET /P "=Trying to %_MsgStr% '%%b' ..." <NUL
netsh interface set interface "%%b" "%_ACTION%" >NUL 2>&1 && ECHO success||ECHO failed
)
)
:_End
PAUSE&GOTO:EOF
:_Validate
:: robust against poisoned user input
SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
IF NOT DEFINED %1 goto:_ExValidate
FOR /L %%a IN (1,1,!%2!) DO IF "!%1!" EQU "%%a" SET "_Var=%%a"
:_ExValidate
IF NOT DEFINED _Var ECHO Invalid input. Digits only from 1 to !%2! are allowed.
ENDLOCAL & SET %3=%_VAR%
GOTO:EOF
...suggestions are welcome.