长时间阅读。第一次发海报。
我有一台带有许多 NIC 的 Windows 计算机,我想要一种简单的方法来显示所连接的适配器的 IP 地址,而不是普通的 ipconfig。
查看本论坛上的之前的问题,我认为以下命令可行,如果有人请指出我如何将双引号传递给最后一个变量:
for /f "tokens=2 delims==" %F in ('wmic nic where "NetConnectionStatus=2 and AdapterTypeId=0" get NetConnectionID /format:list') do netsh interface ip show config name="%F%"
注意:在批处理文件中,它将是 %%F 而不是 %F。
我尝试了几种转义方法,但似乎%F后面的部分无法识别。
预期的输出会是这样的(请忽略西班牙语,IP 地址是感兴趣的信息):
netsh interface ipv4 show config name="Wi-Fi"
Configuración para la interfaz "Wi-Fi"
DHCP habilitado: Sí
Dirección IP: 192.168.1.12
如果您想继续将 WMIC 与 NetSh 结合使用,那么以下内容可能适合您的目的:
直接在cmd.exe中:
For /F Tokens^=6^ Delims^=^" %G In ('%SystemRoot%\System32\wbem\WMIC.exe /NameSpace:\\Root\StandardCimv2 Path MSFT_NetConnectionProfile Get InterfaceAlias /Format:MOF 2^>NUL') Do @For /F "Tokens=2 Delims=:" %H In ('%SystemRoot%\System32\netsh.exe Interface IPv4 Show Config Name^="%G" 2^>NUL ^| %SystemRoot%\System32\find.exe "IP"') Do @For /F %I In ("%H") Do @Echo %I
在批处理文件中:
@Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
For /F Tokens^=6^ Delims^=^" %%G In ('%SystemRoot%\System32\wbem\WMIC.exe
/NameSpace:\\Root\StandardCimv2 Path MSFT_NetConnectionProfile Get
InterfaceAlias /Format:MOF 2^>NUL') Do For /F "Tokens=2 Delims=:" %%H In ('
%SystemRoot%\System32\netsh.exe Interface IPv4 Show Config Name^="%%G" 2^>NUL
^| %SystemRoot%\System32\find.exe "IP"') Do For /F %%I In ("%%H") Do Echo %%I
Pause
EndLocal
但是我更喜欢只使用 WMIC,如下所示:
直接在cmd.exe中:
For /F Tokens^=2^ Delims^=^" %G In ('%SystemRoot%\System32\wbem\WMIC.exe NIC Where "Not NetConnectionStatus Is Null" Assoc /AssocClass:Win32_NetworkAdapterConfiguration 2^>NUL') Do @For /F Tokens^=2^ Delims^=^" %H In ('%SystemRoot%\System32\wbem\WMIC.exe NICConfig Where Index^=%G Get IPAddress') Do @Echo %H
在批处理文件中:
@Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
For /F Tokens^=2^ Delims^=^" %%G In ('%SystemRoot%\System32\wbem\WMIC.exe NIC
Where "Not NetConnectionStatus Is Null" Assoc
/AssocClass:Win32_NetworkAdapterConfiguration 2^>NUL'
) Do For /F Tokens^=2^ Delims^=^" %%H In ('%SystemRoot%\System32\wbem\WMIC.exe
NICConfig Where Index^=%%G Get IPAddress') Do Echo %%H
Pause
EndLocal