我想使用Get-WmiObject Win32_NetworkAdapterConfiguration返回网卡的IP地址。不幸的是,我无法弄清楚如何格式化输出以仅显示IPv.4地址。

Get-WmiObject Win32_NetworkAdapterConfiguration | Select IPAddress | Where-Object {$_.IPaddress -like "192.168*"}

显示:
IPAddress
---------
{192.168.56.1, fe80::8980:15f4:e2f4:aeca}

以上面的输出为例,我希望它仅返回192.168.56.1(某些客户端具有多个NIC,因此是“Where-Object”)

最佳答案

IPAddress属性是string[],因此应执行以下操作:

gwmi Win32_NetworkAdapterConfiguration |
    Where { $_.IPAddress } | # filter the objects where an address actually exists
    Select -Expand IPAddress | # retrieve only the property *value*
    Where { $_ -like '192.168.*' }

关于powershell - PowerShell:格式化Get-WmiObject输出以仅返回IP地址,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/3230924/

10-12 03:33