PowerShell 继承错误

PowerShell Inheritance False

尝试仅显示 IsInherited 为 False 的文件夹

我的代码

$folder = "C:\TZTEST"
$aclentry = get-childitem $folder -recurse -force | get-acl | % {$_.Access}
$manypaths = get-childitem $folder -recurse -force
$aclentry | Format-Table

$newlist = @()

foreach($onepath in $manypaths) {
$acldata=$(get-childitem $folder -recurse -force | get-acl | % {$_.Access})
$itemobject = New-Object –TypeName PSObject
    if ($onepath.Mode -match "d-----" -and $acldata.IsInherited -eq $false -and $acldata.PropagationFlags -eq "InheritOnly") {
        $itemobject | Add-Member –MemberType NoteProperty –Name "Location" -Value $onepath.FullName
        $itemobject | Add-Member –MemberType NoteProperty –Name "IsInherited" -Value $acldata.IsInherited
        $newlist +=$itemobject
        }
}
$newlist

部分有效 当目标文件夹下的所有继承权限的文件夹都没有输出时(正确) 当一个或多个文件夹未继承权限时,输出显示目标文件夹下的所有文件夹(不正确)

文件夹设置 目标文件夹 |-子文件夹 1 - 子文件夹 3 |-子文件夹2

对 3 个子文件夹中的 2 个启用继承(Subfolder1、Subfolder2 enabled)(Subfolder3 disabled)(输出不正确) Folder Permission - Subfolder3 No Inherit

对所有子文件夹启用继承(正确输出) Folder Permission - All Inherit (no output)

我的问题 有什么方法可以只在我的输出中显示没有启用继承的文件夹吗?

在此先感谢您提供的任何帮助或见解。

我认为您只需要所有访问控制条目上 'IsInherited' 为假的文件夹的输出。考虑到这一点,我们只需要评估它们是否包含 $true.

我已经设置了一个类似于您的示例的文件夹路径,并在底部目录中禁用了继承。

C:\Test\Test\NoInherit

Get-ChildItem C:\Test -Recurse | 
? { $_.Attributes -eq [System.IO.FileAttributes]::Directory } | 
? { ((Get-Acl -Path $_.FullName).GetAccessRules($true,$true,[System.Security.Principal.NTAccount]).IsInherited -notcontains $true) -eq $true }