欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Powershell小技巧之复合筛选

程序员文章站 2022-05-08 10:11:12
当你分析文本日志或筛选不通类型的信息时,你通常要使用 where-object。这里有一个通用脚本来说明复合筛选: # logical and filter fo...

当你分析文本日志或筛选不通类型的信息时,你通常要使用 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