简体   繁体   English

是否有一种容易记住的方法来过滤只知道部分 IP 地址的“Get-NetIPAddress”?

[英]Is there an easy to remember way to filter `Get-NetIPAddress` knowing only part of an IP address?

Recently I have been doing more networking work on Windows VM and Containers.最近我在 Windows VM 和容器上做了更多的网络工作。 These instances most easily offer PowerShell access for debugging network issues.这些实例最容易提供 PowerShell 访问以调试网络问题。 I know the basic networking commands, *-Net* , but I am having a hard time filtering the Object based outputs.我知道基本的网络命令*-Net* ,但我很难过滤基于对象的输出。 Specifically I find myself wanting to filter Get-NetIPAddress to just those objects that have IPAddress values in a certain subnet.具体来说,我发现自己想要将Get-NetIPAddress过滤为仅在某个子网中具有 IPAddress 值的那些对象。 This is pretty straight forward when you know the exact IPAddress ( Get-NetIPAddress | where IPAddress -eq 127.0.0.1 ) and I have seen ways to filter by subnet, but not something that is easy to remember.当您知道确切的 IP 地址( Get-NetIPAddress | where IPAddress -eq 127.0.0.1 )并且我已经看到按子网过滤的方法时,这非常简单,但不是很容易记住的东西。 I'd rather not have to look this up every time or install a custom PS module.我宁愿不必每次都查看它或安装自定义 PS 模块。 So my question is, how can you filter the output of Get-NetIPAddress for IP's in a subnet in a way similar to how you know the exact IPAddress: where IPAddress -eq 127.0.0.1 .所以我的问题是,如何以类似于知道确切 IPAddress 的方式过滤子网中 IP 的Get-NetIPAddress输出: where IPAddress -eq 127.0.0.1

For partial matching, you can use the -like or -match operator.对于部分匹配,则可以使用-like-match操作。

-like accepts wildcard matching. -like接受通配符匹配。 * matches any number of characters. *匹配任意数量的字符。 ? matches one of any character.匹配任何字符之一。 [] contains a range of characters to match once. []包含要匹配一次的字符范围。

# Matches 127.0.<anything>
Get-NetIPAddress | where IPAddress -like '127.0.*'

# Matches 127.0.0.<one character>
Get-NetIPAddress | where IPAddress -like '127.0.0.?'

# Matches 127.0.0.<one number between 0 and 9>
Get-NetIPAddress | where IPAddress -like '127.0.0.[0-9]'

-match uses regex. -match使用正则表达式。 This adds more flexibility and complexity.这增加了更多的灵活性和复杂性。 . match any character here so literal dots should be escaped with backslash.匹配此处的任何字符,因此应使用反斜杠对文字点进行转义。

# Matches 127.0.0.<one number between 0 and 9>
Get-NetIPAddress | where IPAddress -match '127\.0\.0\.[0-9]'
# Matches 127.<any number>.<any number>.<any number>
Get-NetIPAddress | where IPAddress -match '127\.[0-9]+\.[0-9]+\.[0-9]+'
# Matches 127.0.<one number between 0 and 9>.<two digit number>
Get-NetIPAddress | where IPAddress -match '127\.0\.[0-9]\.[0-9]{2}'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM