我无法让脚本正常工作。 我有三个阵列。 extensions数组确实可以正确过滤。但是我带有通配符的数组并没有产生我想要的结果。我做错了什么?
# 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
}
}
答案 0 :(得分:3)
您可以编写自己的函数:
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)}
答案 1 :(得分:1)
如果您使用正则表达式很方便,可以通过-Match
比较来完成此操作。将您的$Wildcards =
和$Exclude =
行替换为:
$wildcards = "Help_.*?\.txt|How_.*?\.txt|Recovery\+.*"
$Exclude = "help_text\.txt|Lxr.*?\.exx|help_contents\.txt"
然后您的Where-Object
行:
Where-Object {(($extensions -contains $_.Extension) -or $_.Name -match $wildcards) -and $_.Name -notmatch $exclude}
那应该为你做。 {{3}}上可用$Wildcard =
匹配的说明。