【问题标题】:Get-ChildItem Recursive Exclude NOT Excluding FolderGet-ChildItem 递归排除不排除文件夹
【发布时间】:2015-06-19 09:01:59
【问题描述】:

我查看了其他帖子,甚至关注了那些我无法让它发挥作用的帖子。

我正在尝试从驱动器中提取所有 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]

【问题讨论】:

  • 尝试 Get-ChildItem 'C:*' -Exclude 'Windows'
  • 谢谢,但这似乎也不起作用。我输入的命令行是 .\NTFSPermissions.ps1 "C:\" |导出 Csv C:\Permissions\output.csv -NoTypeInformation。还尝试了排除“Windows”
  • 尝试使用 \NTFSPermissions.ps1 "C:*" | 调用脚本Export-Csv C:\Permissions\output.csv -NoTypeInformation
  • 是的,这正是我所做的 :) 仍然没有运气。事实上,它输出一个小文件,其路径返回为 System.Object[]

标签: powershell get-childitem


【解决方案1】:

使用-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\\?' }

【讨论】:

    【解决方案2】:

    关于-Exclude 参数的几点说明。虽然它没有在文档中明确提到它,但它似乎是基于文件和目录 names....而不是完整路径本身。由于这一点,它不会以任何方式递归地处理我认为是您真正难题的目录。

    由于C:\Windows\* 不是有效的目录名称,因此它不过滤任何内容。 Jisaak$exclude 更改为“windows”的建议在某种意义上确实有效。如果您查看您的输出,您会注意到实际的“c:\windows”文件夹丢失了。您实际上遇到的问题是excludeC:\windows 的子文件夹没有任何作用,我猜这就是您的意图。

    another SO post 关于-Exclude 基本上很烂。只要您了解其局限性,它就会很有用。 Ansgar's answer 涵盖了这一点。它将确保 C:\windows 树中的任何内容都不会出现在您的结果中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-17
      • 2015-02-16
      • 1970-01-01
      • 1970-01-01
      • 2013-02-24
      • 2017-01-01
      • 2012-11-19
      • 1970-01-01
      相关资源
      最近更新 更多