PowerShell:格式化 Get-WmiObject 输出以仅返回 IP 地址

发布于 2024-09-08 19:20:53 字数 412 浏览 4 评论 0原文

我想使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

如果没结果 2024-09-15 19:20:53

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.*' }

The IPAddress property is a string[], so the following should do it:

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.*' }
空城缀染半城烟沙 2024-09-15 19:20:53

添加更快的答案(避免使用Where-Object并在列表上使用类似操作):

@(@(Get-WmiObject Win32_NetworkAdapterConfiguration | Select-Object -ExpandProperty IPAddress) -like "*.*")[0]

希望这有帮助

Adding a quicker answer (avoiding Where-Object and using -like operation on a list):

@(@(Get-WmiObject Win32_NetworkAdapterConfiguration | Select-Object -ExpandProperty IPAddress) -like "*.*")[0]

Hope this Helps

时光瘦了 2024-09-15 19:20:53

我想我会分享我自己对上述内容的变体,以防它对某人有所帮助。就一句话:

Get-WmiObject win32_networkadapterconfiguration | where { $_.ipaddress -like "1*" } | select -ExpandProperty ipaddress | select -First 1

干杯。

Thought I would share my own variation on the above, in case it helps someone. Just one line:

Get-WmiObject win32_networkadapterconfiguration | where { $_.ipaddress -like "1*" } | select -ExpandProperty ipaddress | select -First 1

Cheers.

昔梦 2024-09-15 19:20:53
(Get-WmiObject Win32_NetworkAdapterConfiguration | where { (($_.IPEnabled -ne $null) -and ($_.DefaultIPGateway -ne $null)) } | select IPAddress -First 1).IPAddress[0]

返回与默认网关的网络连接的 IP 地址。
这正是您在大多数情况下所需要的:)

与 Powershell 2.0 (Windows XP) 及更高版本兼容。

(Get-WmiObject Win32_NetworkAdapterConfiguration | where { (($_.IPEnabled -ne $null) -and ($_.DefaultIPGateway -ne $null)) } | select IPAddress -First 1).IPAddress[0]

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.

狠疯拽 2024-09-15 19:20:53

(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 !

生生漫 2024-09-15 19:20:53
(Get-WMIObject -Class Win32_NetworkAdapterConfiguration).IPAddress
(Get-WMIObject -Class Win32_NetworkAdapterConfiguration).IPAddress
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文