简体   繁体   English

Get-ChildItem递归排除不排除文件夹

[英]Get-ChildItem Recursive Exclude NOT Excluding Folder

I've check other posts and even following those I can't get this to function. 我检查了其他帖子,甚至跟随这些帖子我也无法正常运行。

I'm trying to pull all of the ACL information from a drive but exclude the Windows folder. 我正在尝试从驱动器中提取所有ACL信息,但不包括Windows文件夹。

This is the code I'm using, but it always tries to include the folder. 这是我正在使用的代码,但它始终尝试包含该文件夹。 Can someone tell me why this isn't working? 有人可以告诉我为什么这不起作用吗?

I've also tried Where-Object . 我也尝试过Where-Object

$containers = Get-ChildItem -Path $Path -Recurse -Exclude $exclude |
              ? {$_.FullName -notmatch '\\windows\\?'}

Main Code: 主要代码:

function Get-PathPermissions {
    param ( [Parameter(Mandatory=$true)] [System.String]${Path} )

    begin {
        $root = Get-Item $Path 
        ($root | Get-Acl).Access |
            Add-Member -MemberType NoteProperty -Name "Path" -Value $($root.fullname).ToString() -PassThru
    }
    process {
        $exclude = @('C:\Windows\*')
        $containers = Get-ChildItem -Path $Path -Recurse -Exclude $exclude |
                      ? {$_.psIscontainer -eq $true}
        if ($containers -eq $null) {break}
            foreach ($container in $containers)
            {
            (Get-Acl $container.FullName).Access |
                ? { $_.IsInherited -eq $false } |
                Add-Member -MemberType NoteProperty -Name "Path" -Value $($container.fullname).ToString() -PassThru
            }

    }
}

Get-PathPermissions $args[0]

Filtering with -notmatch '\\\\windows\\\\?' 使用-notmatch '\\\\windows\\\\?'过滤 should work. 应该管用。 I'd use the full path, though, to avoid potential undesired exclusions: 不过,我会使用完整路径来避免潜在的意外排除:

$containers = Get-ChildItem -Path $Path -Recurse |
              ? { $_.FullName -notmatch '^c:\\windows\\?' -and $_.PSIsContainer}

On PowerShell v3 or newer you can also use the -Directory switch for restricting the results to directories: 在PowerShell v3或更高版本上,您还可以使用-Directory开关将结果限制为目录:

$containers = Get-ChildItem -Path $Path -Recurse -Directory |
              ? { $_.FullName -notmatch '^c:\\windows\\?' }

Couple points about the -Exclude parameter. 关于-Exclude参数的-Exclude While it does not explicitly mention it in the documentation it seems to work based on file and directory names .... not the full paths themselves. 尽管在文档中没有明确提及它,但它似乎基于文件和目录而不是完整路径本身。 As a result of that point it does not, in any way, work recursively with directories which I think is your actual conundrum. 因此,它不能以任何方式递归地处理我认为是您实际难题的目录。

Since C:\\Windows\\* is not a valid directory name that is why it is not filtering anything. 由于C:\\Windows\\*不是有效的目录名称,因此它不过滤任何内容。 Jisaak 's suggestion of changing $exclude to just "windows" did work in a sense. Jisaak的建议将$exclude更改$exclude仅“ windows”在某种意义上确实有效。 If you looked at your output you would have noticed that the actual "c:\\windows" folder was missing. 如果查看输出,您会注意到实际的“ c:\\ windows”文件夹已丢失。 What you are actually having a problem with is that exclude does nothing for the sub folders of C:\\windows which I will guess is what you intended. 您实际上遇到的问题是, excludeC:\\windows的子文件夹没有任何作用,我想这正是您的意图。

There is another SO post about how -Exclude basically sucks. 还有另一篇关于-Exclude基本如何烂的文章。 It can be useful as long as you understand its limitations. 只要您了解其局限性,它就很有用。 Ansgar's answer covers the way around that. 安斯加(Ansgar)的答案涵盖了解决方法。 It will make sure nothing in the tree of C:\\windows ends up in your results. 它将确保C:\\windows树中的任何内容都不会最终结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM