PowerShell 技能连载 - 理解顺序过滤

适用于 PowerShell 所有版本

当您在解析基于文本的日志文件时,或是需要过滤其它类型的信息时,往往需要使用 Where-Object 命令。以下是一些常见的场景,演示如何合并过滤器:

# logical AND filter for ALL keywords
Get-Content -Path C:\windows\WindowsUpdate.log |
  Where-Object { $_ -like '*successfully installed*' } |
  Where-Object { $_ -like '*framework*' } |
  Out-GridView

# above example can also be written in one line
# by using the -and operator
# the resulting code is NOT faster, though, just harder to read
Get-Content -Path C:\windows\WindowsUpdate.log |
  Where-Object { ($_ -like '*successfully installed*') -and ($_ -like '*framework*') } |
  Out-GridView

# logical -or (either condition is met) can only be applied in one line
Get-Content -Path C:\windows\WindowsUpdate.log |
  Where-Object { ($_ -like '*successfully installed*') -or ($_ -like '*framework*') } |
  Out-GridView
作者

吴波

发布于

2014-08-22

更新于

2022-07-06

许可协议

评论