启用/禁用以太网的批处理,用于一个网卡切换本地网络和wifi使用(Win10)
时间:2022-03-18 10:34
注意下面时英文版上默认网络使用,同时接入了网线和wifi时,本地网络优先wifi。
所以禁用本地网络就会自动连接到wifi,启用本地网络,就会禁用wifi。
批处理支持 -slice 参数,代码如下:
SwitchEthernet.bat
1 @echo off 2 pushd "%~dp0" 3 set name="Ethernet" 4 set findtext="Administrative state: Enabled" 5 6 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\SYSTEM" >nul 2>nul 7 if %errorlevel%==0 goto :START 8 9 echo ** Please use administrator account to run. ** 10 goto :END 11 12 :START 13 set input= 14 set slice=0 15 if "%1"=="-slice" set slice=1 16 netsh interface show interface name=%name% 17 netsh interface show interface name=%name% | find %findtext% > nul 18 if ERRORLEVEL 1 goto :OP_ENABLED 19 :OP_DISABLED 20 echo. 21 if %slice% equ 1 goto :op_d_skip 22 set /p input=Input ‘Y‘ to DISABLE interface %name% [y/N]: 23 goto :op_d_run 24 :op_d_skip 25 echo Input ‘Y‘ to DISABLE interface %name% [y/N]:y 26 set input=Y 27 :op_d_run 28 if /i "%input%"=="Y" netsh interface set interface name=%name% admin=DISABLED 29 goto :END 30 :OP_ENABLED 31 echo. 32 if %slice% equ 1 goto :op_e_skip 33 set /p input=Input ‘Y‘ to ENABLED interface %name% [y/N]: 34 goto :op_e_run 35 :op_e_skip 36 echo Input ‘Y‘ to ENABLED interface %name% [y/N]:y 37 set input=Y 38 :op_e_run 39 if /i "%input%"=="Y" netsh interface set interface name=%name% admin=ENABLED 40 goto :END 41 :END 42 echo. 43 echo Current Status: 44 netsh interface show interface 45 if %slice% equ 1 goto :skip_any_key 46 echo Press any key to exit... 47 pause > nul 48 goto :end1 49 :skip_any_key 50 echo Will exit in 5 seconds... 51 ping 127.1 -n 6 > nul 52 :end1 53 popd