Strange results quering WMI information.

huangapple go评论54阅读模式
英文:

Strange results quering WMI information

问题

我写了一个简单的批处理文件来快速检查网络接口状态:

@echo off
cls
setlocal
for /f "tokens=2 delims==" %%q in ('wmic path win32_networkadapter where "netconnectionid like '%%ovpn_wintun%%'" get netconnectionstatus /format:list ^| find /i "NetConnectionStatus"') do (
echo q variable is: %%q
IF %%q LEQ 2 echo 1. q less or equal %%q
IF %%q EQU 2 echo 2. q equal %%q
IF %%q GEQ 2 echo 3. q greater or equal %%q
IF %%q LEQ 3 echo 4. q less or equal %%q
IF %%q EQU 3 echo 5. q equal %%q
IF %%q GEQ 3 echo 6. q greater or equal %%q
)
endlocal
pause

并且收到了意外的结果:

Strange results quering WMI information.

将变量作为字符串进行比较没有结果。

请问有人可以解释一下,这是什么情况,是否可以在批处理脚本中修复它?

英文:

I write simple batch file to fast check network interface status:

@echo off
cls
setlocal
for /f "tokens=2 delims==" %%q in ('wmic path win32_networkadapter where "netconnectionid like '%%ovpn_wintun%%'" get netconnectionstatus /format:list ^| find ^/i ^"NetConnectionStatus^"') do (
echo q variable is: %%q
IF %%q LEQ 2 echo 1. q less or equal %%q
IF %%q EQU 2 echo 2. q equal %%q
IF %%q GEQ 2 echo 3. q greater or equal %%q
IF %%q LEQ 3 echo 4. q less or equal %%q
IF %%q EQU 3 echo 5. q equal %%q
IF %%q GEQ 3 echo 6. q greater or equal %%q
)
endlocal
pause

And receive unexpected results:

Strange results quering WMI information.

Compare variables as string have no result.

Can someone explain me, please, what is it and it is fixable in batch script?

答案1

得分: 0

以下是翻译的代码部分:

问题的原因是 wmic 输出不仅是Unicode,而且还包含带有行终止符的输出,如<kbd>CR</kbd><kbd>CR</kbd><kbd>LF</kbd>。

您可以通过将 %%q 输出到文件并使用 editplus 或类似的真实文本编辑器检查该文件来观察到这一点。

这是一个快速修复方法(请注意,我已经修改了 wmic 命令以适应我的系统)

@ECHO OFF
SETLOCAL
for /f "tokens=2 delims==" %%q in ('wmic path win32_networkadapter where "netconnectionid like '%%ethernet%%'" get netconnectionstatus /format:list ^| find /i "NetConnectionStatus"') do (
 echo q variable is: %%q
 SET /a QQ=%%q
 SETLOCAL enabledelayedexpansion
 IF !qq! LEQ 2 echo 1. q less or equal %%q
 IF !qq! EQU 2 echo 2. q equal %%q
 IF !qq! GEQ 2 echo 3. q greater or equal %%q
 IF !qq! LEQ 3 echo 4. q less or equal %%q
 IF !qq! EQU 3 echo 5. q equal %%q
 IF !qq! GEQ 3 echo 6. q greater or equal %%q
 endlocal
)

SET Q
GOTO :EOF
英文:

The issue is caused by the fact that wmic output is not only Unicode, but also has an output with line terminals like <kbd>CR</kbd><kbd>CR</kbd><kbd>LF</kbd>.

You could observe this by outputting %%q to a file and examining the file with editplus or a similar real text editor.

Here's a quick-fix (noting that I've modified the wmic command to suit my system)

@ECHO OFF
SETLOCAL
for /f &quot;tokens=2 delims==&quot; %%q in (&#39;wmic path win32_networkadapter where &quot;netconnectionid like &#39;%%ethernet%%&#39;&quot; get netconnectionstatus /format:list ^| find ^/i ^&quot;NetConnectionStatus^&quot;&#39;) do (
 echo q variable is: %%q
 SET /a QQ=%%q
 SETLOCAL enabledelayedexpansion
 IF !qq! LEQ 2 echo 1. q less or equal %%q
 IF !qq! EQU 2 echo 2. q equal %%q
 IF !qq! GEQ 2 echo 3. q greater or equal %%q
 IF !qq! LEQ 3 echo 4. q less or equal %%q
 IF !qq! EQU 3 echo 5. q equal %%q
 IF !qq! GEQ 3 echo 6. q greater or equal %%q
 endlocal
)

SET Q
GOTO :EOF

huangapple
  • 本文由 发表于 2023年5月14日 01:26:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/76244062.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定