Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell 其中对象和数组未正确过滤_Powershell - Fatal编程技术网

Powershell 其中对象和数组未正确过滤

Powershell 其中对象和数组未正确过滤,powershell,Powershell,我的脚本无法正常运行。 我有三个数组。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"

我的脚本无法正常运行。 我有三个数组。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
    }
}

如果你能熟练使用正则表达式,你可以通过
-Match
比较来实现。将
$Wildcards=
$Exclude=
行替换为:

$wildcards = "Help_.*?\.txt|How_.*?\.txt|Recovery\+.*"
$Exclude = "help_text\.txt|Lxr.*?\.exx|help_contents\.txt"
然后,您的
Where对象
与以下内容对齐:

    Where-Object {(($extensions -contains $_.Extension) -or $_.Name -match $wildcards) -and $_.Name -notmatch $exclude}

那应该对你有用。有关
$Wildcard=
匹配的说明,请访问。

您可以编写自己的函数:

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)}

$\.Name-类似于$wildcards
正在将
$\.Name
与数组进行比较。您需要将
$\uName
与数组中的每个值进行比较。因此,我需要为每个值添加一个-or和-and语句?[0]、[1]、[2]等?没有捷径可走?我希望数组可以工作,而不是必须在where子句中指定每个值。这样做:
($wildcards |%{“help_a.txt”-类似$})。包含($true)
。也许有一种更简洁的方法。我硬编码了一个文件名来测试它,但我想你明白了。谢谢Jason。工作完美。我喜欢这个功能。它的工作原理就像我希望我的原始脚本能够工作一样。我只需要理解并破译函数中的措辞。我会做一些研究。这也行。我只是在正则表达式方面没有经验。谢谢你的有用参考资料。我会花一些时间来更好地理解它。使用正则表达式确实需要一些学习,但它们在尝试解析或匹配文本和模式时非常强大,与其他一些方法相比,它们速度非常快。我很高兴你找到了一个适合你的答案!