windows - 是否有一种容易记住的方法来过滤仅知道部分 IP 地址的“Get-NetIPAddress”?
问题描述
最近我一直在做更多关于 Windows VM 和容器的网络工作。这些实例最容易为调试网络问题提供 PowerShell 访问。我知道基本的网络命令,*-Net*
但是我很难过滤基于对象的输出。具体来说,我发现自己想过滤Get-NetIPAddress
到那些在某个子网中具有 IPAddress 值的对象。当您知道确切的 IPAddress ( Get-NetIPAddress | where IPAddress -eq 127.0.0.1
) 并且我已经看到了按子网过滤的方法时,这非常简单,但不是很容易记住的东西。我宁愿不必每次都查看它或安装自定义 PS 模块。所以我的问题是,如何 Get-NetIPAddress
以类似于知道确切 IPAddress: 的方式过滤子网中 IP 的输出where IPAddress -eq 127.0.0.1
。
解决方案
对于部分匹配,您可以使用-like
or-match
运算符。
-like
接受通配符匹配。*
匹配任意数量的字符。?
匹配任何字符之一。[]
包含要匹配一次的字符范围。
# 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
使用正则表达式。这增加了更多的灵活性和复杂性。.
匹配此处的任何字符,因此应使用反斜杠对文字点进行转义。
# 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}'
推荐阅读
- c# - 如何在 Windows 10 中安装最新的 OLEDB?
- android - FCM 通知的奇怪行为
- swift - 如何更新 MDQuery 中的查询字符串?
- npm - 类扩展值未定义的 npm 构建失败不是构造函数或 null
- pg-promise - pgpromise 插入返回消息失败,但成功插入到表中
- javascript - React Native,从 ImageManipulator 生成的 url 获取数据到张量
- r - 更改 ggplot2 中多面箱线图的图例,使其内部具有相似名称的组
- android-drawable-importer - Android Studio北极狐版中的Android Drawable Importer
- sql - 如何将函数嵌入到 PostgreSQL 的列中
- vue.js - VuePress:Exery 单页侧边栏中的自定义内容