Skip to content

Instantly share code, notes, and snippets.

@LiuJiewenTT
Created August 14, 2023 12:40
Show Gist options
  • Select an option

  • Save LiuJiewenTT/cb32dd766870e6ef3bee7d86b21b07d9 to your computer and use it in GitHub Desktop.

Select an option

Save LiuJiewenTT/cb32dd766870e6ef3bee7d86b21b07d9 to your computer and use it in GitHub Desktop.
Vitual Network Adapter Manager
@echo off
if not defined str_pattern (
set str_pattern=VMware Network Adapter
)
set "action=%1"
if /I "%action%" == "" (
echo. Need a selected action.
call:help
goto:eof
)
if "%1" == "--help" set help_flag=true
if "%1" == "/help" set help_flag=true
if "%1" == "/h" set help_flag=true
if "%1" == "/?" set help_flag=true
if defined help_flag (
call:help
set help_flag=<nul
goto:eof
)
echo.Action= [%action%]
echo.str_pattern= [%str_pattern%]
echo.Before:
for /f "usebackq tokens=1,2,4*" %%i in (`netsh int show interface ^| findstr "%str_pattern%"`) do (
echo [%%k %%l]: %%i, %%j
)
if /I "%action%" == "disconnect" (
for /f "usebackq tokens=1,2,4*" %%i in (`netsh int show interface ^| findstr "%str_pattern%"`) do (
call:disconnect_adapter "%%k %%l"
)
) else if /I "%action%" == "connect" (
for /f "usebackq tokens=1,2,4*" %%i in (`netsh int show interface ^| findstr "%str_pattern%"`) do (
call:connect_adapter "%%k %%l"
)
) else if /I "%action%" == "disable" (
for /f "usebackq tokens=1,2,4*" %%i in (`netsh int show interface ^| findstr "%str_pattern%"`) do (
call:disable_adapter "%%k %%l"
)
) else if /I "%action%" == "enable" (
for /f "usebackq tokens=1,2,4*" %%i in (`netsh int show interface ^| findstr "%str_pattern%"`) do (
call:enable_adapter "%%k %%l"
)
) else (
echo.Invalid Action.
@echo on
@ goto:eof
)
echo.After:
for /f "usebackq tokens=1,2,4*" %%i in (`netsh int show interface ^| findstr "%str_pattern%"`) do (
echo [%%k %%l]: %%i, %%j
)
@echo on
@ goto:eof
:help
echo.Program Name: Vitual Network Adapter Manager
echo.Author: "LiuJiewenTT (on Github.com)"
echo.Date: 2023-08-14 19:09:00 +0800
echo.Description: This script is to manage Virtual Lan Adapters, typically supports VMwares' and Hyper-Vs'
echo.Actions: DISCONNECTED ^| CONNECTED ^| DISABLED ^| ENABLED
echo.Take one of the actions to execute
goto:eof
:disconnect_adapter
@REM echo [%~1]
netsh int set interface name="%~1" admin=ENABLED connect=DISCONNECTED
goto:eof
:connect_adapter
@REM echo [%~1]
netsh int set interface name="%~1" admin=ENABLED connect=CONNECTED
goto:eof
:disable_adapter
@REM echo [%~1]
netsh int set interface name="%~1" admin=DISABLED
goto:eof
:enable_adapter
@REM echo [%~1]
netsh int set interface name="%~1" admin=ENABLED
goto:eof
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment