powershell - 简单的PowerShell选择字符串模式

标签 powershell

为什么不使用PowerShell脚本

Get-NetRoute | Select-String -Pattern "255"
Get-NetRoute | Select-String -Pattern 255
Get-NetRoute | Select-String -Pattern '255'
Get-NetRoute | Select-String -Pattern '.*255.*'

给出任何结果?模式有什么问题?

最佳答案

Select-String cmdlet设计用于字符串对象。 Get-NetRoute cmdlet的输出是一个数组。

enter image description here

为了使代码正常工作,您必须将其转换为这样的string对象。

(Get-NetRoute | out-string).split("`n") | Select-String -Pattern "255"

但是我怀疑如果您打算在结果中使用结果会很有帮助。

您真正需要的是Where-object
Get-NetRoute | Where-Object {$_.DestinationPrefix -like "*255*"}。希望能有所帮助。

关于powershell - 简单的PowerShell选择字符串模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60391315/

相关文章:

C++通过命令行使用*字符传递多个文件

powershell - 尝试在带有 IE 11 的 Powershell 中使用 getElementById,来自 HRESULT : 0x800A01B6 的错误异常

powershell - 使用 PowerShell FTP 下载多个文件

xml - 添加 XML 子元素

xml - 使用 PowerShell 更新 XML 值

parsing - Powershell中的动态字符串解析

powershell - 使用 Powershell 更改 DCOM 配置安全设置

powershell - Powershell 中的后台作业

variables - 如何将变量的值用作变量本身?

powershell - 一行输出到txt和cvs