PowerShell:格式化 Get-WmiObject 输出以仅返回 IP 地址
我想使用 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”)
I would like to use Get-WmiObject Win32_NetworkAdapterConfiguration to return the ip address of a network card. Unfortunately, I cannot figure out how to format the output to display only the IPv.4 address.
Get-WmiObject Win32_NetworkAdapterConfiguration | Select IPAddress | Where-Object {$_.IPaddress -like "192.168*"}
Displays:
IPAddress
---------
{192.168.56.1, fe80::8980:15f4:e2f4:aeca}
Using the above output as an example, I would like it to only return 192.168.56.1 (Some clients have multiple NIC's, hence the "Where-Object")
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
IPAddress
属性是一个string[]
,因此应该执行以下操作:The
IPAddress
property is astring[]
, so the following should do it:添加更快的答案(避免使用Where-Object并在列表上使用类似操作):
希望这有帮助
Adding a quicker answer (avoiding Where-Object and using -like operation on a list):
Hope this Helps
我想我会分享我自己对上述内容的变体,以防它对某人有所帮助。就一句话:
干杯。
Thought I would share my own variation on the above, in case it helps someone. Just one line:
Cheers.
返回与默认网关的网络连接的 IP 地址。
这正是您在大多数情况下所需要的:)
与 Powershell 2.0 (Windows XP) 及更高版本兼容。
Returns the IP-address of network connection with default gateway.
This is exactly what you need in most cases :)
Compatible with Powershell 2.0 (Windows XP) and newer.
(Get-WmiObject win32_Networkadapterconfiguration |Where-Object{$_.ipaddress -notlike $null}).IPaddress | Select-Object -First 1
希望这会有所帮助!
(Get-WmiObject win32_Networkadapterconfiguration | Where-Object{$_.ipaddress -notlike $null}).IPaddress | Select-Object -First 1
Hope that this will help !