PowerShell:格式化 Get-WmiObject 输出以仅返回 IP 地址 [英] PowerShell: Format Get-WmiObject output to return only the IP address

查看:82
本文介绍了PowerShell:格式化 Get-WmiObject 输出以仅返回 IP 地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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*"}

显示:

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

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

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")

推荐答案

IPAddress 属性是一个 string[],所以下面应该这样做:

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

这篇关于PowerShell:格式化 Get-WmiObject 输出以仅返回 IP 地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆