Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell 从cmdlet中选择数组的索引_Powershell - Fatal编程技术网

Powershell 从cmdlet中选择数组的索引

Powershell 从cmdlet中选择数组的索引,powershell,Powershell,我正在尝试通过以下方式获取本地IP地址: gwmi Win32_NetworkAdapterConfiguration | Select Description,IPAddress | ?{$_.IPAddress -ne $null} 现在,这给出了适配器的描述(名称),以及字符串数组中的IPv4地址和IPv6地址。如何在Select cmdlet中仅选择IPv4地址?我尝试了很多方法,但似乎找不到解决办法 谢谢 您可以将-match与正则表达式模式一起使用,该正则表达式模式与calcula

我正在尝试通过以下方式获取本地IP地址:

gwmi Win32_NetworkAdapterConfiguration | Select Description,IPAddress | ?{$_.IPAddress -ne $null}
现在,这给出了适配器的描述(名称),以及字符串数组中的IPv4地址和IPv6地址。如何在Select cmdlet中仅选择IPv4地址?我尝试了很多方法,但似乎找不到解决办法


谢谢

您可以将
-match
与正则表达式模式一起使用,该正则表达式模式与calculate属性中的IPv4地址相匹配:

$IPv4Pattern = '^(\d{1,3}\.){3}\d{1,3}$'
gwmi Win32_NetworkAdapterConfiguration | Select Description,@{Name="IPAddress";Expr={$_.IPAddress|Where{ $_ -match $IPv4Pattern}}} | ?{$_.IPAddress -ne $null}

要修改您现在的语法,这里有一个解决方案<代码>gwmi Win32_NetworkAdapterConfiguration |?{$\.IPAddress-ne$null}选择描述,@{n='IPAddress';e={$\.IPAddress[0]}Hm,有趣。Mathias感谢您:这将在只有IPv6地址的适配器上产生意想不到的结果。这正是我所需要的。谢谢您!