如何使用 get-netipaddress 和 where-object 获取特定网络适配器的详细信息

how to get specific network adapter details with get-netipaddress and where-object

我试图只获取我的一个以太网适配器的 IPv4 地址详细信息。我需要用 where-object 过滤结果。但是每当我尝试向 where 添加多个参数时,我都会收到错误消息。

Get-NetIpAddress | where { $_.Interfaceindex -EQ 2 -and $_.AddressFamily -EQ IPv4 }

此命令显示此错误。

我只需键入即可获得相同的结果:

Get-NetIPAddress -InterfaceIndex 2 -AddressFamily IPv4

但我需要使用 where-object 来获取结果。有什么办法吗?我刚学powershell

试试这个,我已经添加了引号,因为你在引号中漏掉了它们。我还在方括号中添加了 - 并且它在视觉上更具吸引力,可以看到您对代码的每个部分做了什么。

Get-NetIpAddress | Where-Object {($_.Interfaceindex -eq "2") -and ($_.AddressFamily -eq "IPv4")}

只需将引号添加到字符串 ipv4

Get-NetIpAddress | where { $_.Interfaceindex -EQ 2 -and $_.AddressFamily -EQ "IPv4" }