我无法让我的脚本正常工作。 我有三个数组。扩展数组确实可以正确过滤。然而,我的带有通配符的数组没有生成我想要的结果。我做错了什么?
# Read List of Servers from flat file
$data=Get-Content C:\myscripts\admin_servers.txt
# Variables that will be used against search parameter
$extensions = @(".ecc", ".exx", ".ezz", ".vvv")
$wildcards = @("Help_*.txt", "How_*.txt", "Recovery+*")
$exclude = @("help_text.txt", "Lxr*.exx", "help_contents.txt")
# Loop each server one by one and do the following
foreach ($server in $data)
{
# Search the server E:\ and all subdirectories for the following types of
# extensions or wildcards.
Get-ChildItem -path \\$server\e$ -Recurse | Where-Object {
(($extensions -contains $_.Extension) -or $_.Name -like $wildcards) -and
$_.Name -notlike $exclude
}
}
最佳答案
您可以编写自己的函数:
function Like-Any {
param (
[String]
$InputString,
[String[]]
$Patterns
)
foreach ($pattern in $Patterns) {
if ($InputString -like $pattern) {
return $true
}
}
$false
}
然后这样调用它:
Get-ChildItem -path \\$server\e$ -Recurse |
Where-Object { `
(($extensions -contains $_.Extension) -or (Like-Any $_.Name $wildcards)) `
-and !(Like-Any $_.Name $exclude)}
关于powershell - Where-Object 和数组未正确过滤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35708274/