【发布时间】:2018-03-08 22:23:51
【问题描述】:
我必须通过某些字符串过滤我的结果,并尝试使用 -match 和 -contains 来实现。
-match 如果我只有一个要过滤的值,但不适用于数组。
-contains 既不适用于一个字符串,也不适用于字符串数组。
为什么它不能使用多个值?尤其是-contains。或者有其他简单的方法可以解决吗?
$Folder = 'C:\Test'
$filterArray = @('2017-05', '2017-08')
$filter = '2017-05'
## test with -MATCH
## working with one match string
Get-ChildItem -Path $Folder -Recurse -Include *.txt |
Where { $_.FullName -match $filter } |
ForEach-Object { $_.FullName }
## NOT working with match string array - no results
Get-ChildItem -Path $Folder -Recurse -Include *.txt |
Where { $_.FullName -match $filterArray } |
ForEach-Object { $_.FullName }
## test with -CONTAINS
## NOT working with one contains string - no results
Get-ChildItem -Path $Folder -Recurse -Include *.txt |
Where { $_.FullName -contains $filter } |
ForEach-Object { $_.FullName }
## NOT working with contains string array- no results
Get-ChildItem -Path $Folder -Recurse -Include *.txt |
Where { $_.FullName -contains $filterArray } |
ForEach-Object { $_.FullName }
【问题讨论】:
-
相关(
-Contains(集合运算符)、-match/-imatch(正则表达式匹配)和-like/-ilike(类SQL匹配)之间的区别):PowerShell and the -contains operator