仅在连接到wifi Batch时运行代码



只有当它连接到Wifi,而不是蓝牙LAN 时,我才需要运行批处理文件

我有这个代码,但它返回这个,并且在没有互联网连接的情况下仍然运行代码

Node - DEVICENAME
ERROR:
Description = Invalid query

代码:

@echo off
For /f "usebackq" %%A in (
`wmic path WIN32_NetworkAdapter where 'NetConnectionID="Wi-Fi"' get NetConnectionStatus`
) do if %%A equ 7 (goto end)
<code to run>
:end

您不需要for循环:

wmic path WIN32_NetworkAdapter where 'NetConnectionID="Wi-Fi"' get NetConnectionStatus |find "7" >nul && goto :eof
echo code to run

如果您想使其更安全,请使用findstr /rc:"^7 *$"而不是find "7"

(您原来的方法失败了,因为必须转义=... where 'NetConnectionID^="Wi-Fi"' get ...,并且由于wmic输出异常,%%A中有CR,这打乱了if语法;您可以看到echo on的两个问题(至少您可以看到奇怪的事情发生(

WMIC的输出也是我使用的那个奇怪的findstr模式的原因。(在7之后有尾随空格(。

如果您还不知道无线接口连接的名称(这是一个可配置的属性(,那么您可能会使用类似的东西:

@For /F Tokens^=6^ Delims^=^" %%G In ('%SystemRoot%System32wbemWMIC.exe NIC
Where "Not NetConnectionID Is Null And NetConnectionStatus='2'" Get
NetConnectionID /Format:MOF 2^>NUL') Do @%SystemRoot%System32netsh.exe WLAN^
Show Interfaces 2>NUL | %SystemRoot%System32findstr.exe /E /L ": %%G" 1>NUL^
&& <code to run>

如果您的目标系统仍在使用Windows 7,(在查找/Format选项中使用的一些XSL文件时存在已知问题(,则以下替代方案可能适用于您:

@For /F "Skip=1 Delims=" %%G In ('%SystemRoot%System32wbemWMIC.exe NIC Where
"Not NetConnectionID Is Null And NetConnectionStatus='2'" Get NetConnectionID
2^>NUL') Do @For /F "Tokens=*" %%H In ("%%G") Do @%SystemRoot%System32netsh.exe^
WLAN Show Interfaces 2>NUL | %SystemRoot%System32findstr.exe /E /L ": %%H" 1>NUL^
&& <code to run>

很明显,您会将上面提供和复制的<code to run>更改为一个或多个实际有效的命令

相关内容

最新更新