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

标签 powershell get-childitem

我检查了其他帖子,甚至跟随这些帖子,我也无法正常运行。

我正在尝试从驱动器中提取所有ACL信息,但不包括Windows文件夹。

这是我正在使用的代码,但它始终尝试包含该文件夹。有人可以告诉我为什么这不起作用吗?

我也尝试过Where-Object

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

主要代码:

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]

最佳答案

使用-notmatch '\\windows\\?'进行过滤应该可以。不过,我会使用完整路径来避免潜在的意外排除:

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

在PowerShell v3或更高版本上,您还可以使用-Directory开关将结果限制为目录:

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

关于powershell - Get-ChildItem递归排除不排除文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30934080/

相关文章:

powershell - Get-ChildItem为Assembly。*。dll添加排除筛选器,但包括Assembly.Some。*。dll

node.js - Windows 上的 TestCafe 失败,并出现 spawn powershell.exe ENOENT 错误

powershell - 如何在PowerShell中使用HttpUtility.UrlEncode方法重命名文件?

powershell - Get-childitem Lastwritetime未正确返回

powershell - 按文件名 Powershell 中的日期列出文件

powershell - 为什么powershell远程设置COM+应用的 'QueuingEnabled'会导致保存失败?

设置了 content-md5 的 Azure blob 上传

windows - 将小的 PS 脚本转换为 .BATch 文件中的长行

powershell - 如何测试Get-ChildItem是否没有结果(零文件)?