启用/禁用网络适配器的批处理文件

问题描述 投票:0回答:2

我有一个在工作中很有用的脚本,可以用来更改 LAN 适配器静态/DHCP,效果很好。但是,在通过网络使用命令时,我们偶尔需要禁用网络适配器(以防不同网络上发生冲突)。 这是我获得的启用/禁用命令的代码。

:2
@echo off
netsh wlan show networks | FIND "Wireless network connection" /I /C >NUL 2>NUL
IF %errorlevel% equ 1 (netsh interface set interface "Wireless network connection" DISABLED)
IF %errorlevel% equ 0 (netsh interface set interface "Wireless network connection" ENABLED)
exit /b

当我单独运行 netsh 命令时,确实执行正确,这意味着我的 if 语句有问题。

当网络适配器启用时:

netsh wlan show networks | FIND "Wireless network connection" /I /C
1

当网络适配器被禁用时:

netsh wlan show networks | FIND "Wireless network connection" /I /C
0

运行整个代码时,每次都运行通过(无论无线网络适配器的状态如何,都返回1)。 大家有什么建议吗?

windows batch-file cmd
2个回答
0
投票
echo Errorlevel was %errorlevel%
IF %errorlevel% equ 1 (
 echo was enabled
 netsh interface set interface "Wireless network connection" DISABLED
) else (
 echo was disabled
 IF %errorlevel% equ 0 (
   netsh interface set interface "Wireless network connection" ENABLED
 )
)

正如您目前所拥有的那样,第二个

errorlevel
将解释第一个
netsh
的结果(如果生效)。


0
投票

不要混淆命令输出和

%errorlevel%
:

C:> echo yes|find "yes" /c
1

C:> echo %errorlevel%
0

C:> echo yes|find "no" /c
0

C:> echo %errorlevel%
1
当最后一个命令 (

%errorlevel%

) 不成功时,
find
被设置,而
find /c
返回结果的计数。没有发现意味着
find /c
返回
0
并且
%errorlevel%
为 1。

© www.soinside.com 2019 - 2024. All rights reserved.