英文:
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
并且收到了意外的结果:
将变量作为字符串进行比较没有结果。
请问有人可以解释一下,这是什么情况,是否可以在批处理脚本中修复它?
英文:
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:
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 "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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论