找回密码
 注册
搜索
[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
查看: 22511|回复: 2

[其他] [已解决]批处理调用wmic获取IP和MAC变量显示不正常

[复制链接]
发表于 2021-6-13 01:17:46 | 显示全部楼层 |阅读模式
  1. @echo off
  2. setlocal EnableDelayedExpansion
  3. for /f "tokens=2 delims==" %%i in ('Wmic Nic Where "NetConnectionID<>Null And PNPDeviceID Like '[PR]%%'" Get MACAddress /Value^|find "="') do (
  4.     set /a Sum+=1
  5.     for /f "tokens=2 delims={,}" %%j in ('Wmic NicConfig Where "IPEnabled=True And MACAddress='%%i'" Get IPAddress /Value 2^>nul^|find "="') do (
  6.         set IP!Sum!=%%~j
  7.         set MAC!Sum!=%%i
  8.     )
  9. )
  10. for /l %%i in (1,1,%Sum%) do echo;!MAC%%i! !IP%%i!
  11. pause
复制代码
改成 for /l %%i in (1,1,%Sum%) do echo;!IP%%i! !MAC%%i! 就正常显示了,这是什么原因?

通过楼下的回答,使用变量截取倒数几位可以证实获取到的MACAddress确实存在其他不可见的字符,原因找到就好办了
  1. @echo off
  2. setlocal EnableDelayedExpansion
  3. for /f "tokens=2 delims==" %%i in ('Wmic Nic Where "NetConnectionID<>Null And PNPDeviceID Like '[PR]%%'" Get MACAddress /Value^|find "="') do (
  4.     set /a Sum+=1
  5.     set MAC=%%i
  6.     set MAC=!MAC::=-!
  7.     for /f "tokens=2 delims={,}" %%j in ('Wmic NicConfig Where "IPEnabled=True And MACAddress='%%i'" Get IPAddress /Value 2^>nul^|find "="') do (
  8.         set IP!Sum!=%%~j
  9.         set MAC!Sum!=!MAC:~,17!
  10.     )
  11. )
  12. for /l %%i in (1,1,%Sum%) do echo;!MAC%%i! !IP%%i!
  13. pause
复制代码
发表于 2021-6-13 08:45:01 | 显示全部楼层
!MAC%%I!有非打印字符CR
将第7行改为
  1. set MAC=%%i
  2. set MAC!Sum!=!MAC:~,1!
复制代码

评分

参与人数 1技术 +1 收起 理由
不知道是谁 + 1 技术

查看全部评分

发表于 2021-6-14 12:37:22 | 显示全部楼层
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|批处理之家 ( 渝ICP备10000708号 )

GMT+8, 2026-3-20 17:17 , Processed in 0.031294 second(s), 18 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表