PowerShell Get-Acl-获取成员而不是组

时间:2018-09-03 13:41:19

标签: powershell acl

在PowerShell中使用Get-Acl时,如何显示属于组的所有成员而不是组本身?

所以:

Get-ChildItem C:\ | where-object {($_.PsIsContainer)} | Get-Acl | select path -ExpandProperty Access

显示如下内容:

Path              : Microsoft.PowerShell.Core\FileSystem::C:\Test
FileSystemRights  : FullControl
AccessControlType : Allow
IdentityReference : BUILTIN\Administrators
IsInherited       : False
InheritanceFlags  : ContainerInherit, ObjectInherit
PropagationFlags  : None

Path              : Microsoft.PowerShell.Core\FileSystem::C:\Test
FileSystemRights  : ReadAndExecute, Synchronize
AccessControlType : Allow
IdentityReference : BUILTIN\Users
IsInherited       : False
InheritanceFlags  : ContainerInherit, ObjectInherit
PropagationFlags  : None

相反,我希望它列出所有属于每个文件夹权限的Administrators / Users用户,并丢弃该组。

又如何将Convert-Path添加到select path语句中,以使显示的路径仅为C:\ Test?

谢谢!

1 个答案:

答案 0 :(得分:0)

我无法通过链接发布和/或PowerShell Access Control模块来解决它,但仍然只有组。因此,最终我可以通过结合其他各种有用的帖子获得所需的信息,例如:

PowerShell script to return members of multiple security groups
List user details from Username

扩展我的原始问题,包括我想要的最终结果,这就是我的方法。它不是很漂亮(即使重复一小段代码),并且大部分可能会放在一行中,但是仅凭我自己的可读性,这种方式还是有意义的。另外,由于发现了有用的信息,我也省略了丢弃组的操作。

$queryPath = "C:\Test"
$targetFile = "C:\Test.csv"

$Table = @()

$Record = [ordered]@{
    "Path" = ""
    "IdentityReference" = ""
    "Class" = ""
    "GrpMember" = ""
}

$foldersToQuery = Get-ChildItem $queryPath | Where {$_.PSIsContainer} | select -expandproperty FullName

foreach ($folder in $foldersToQuery) {
    $Record.Path = $folder
    $permissions = Get-Acl $folder | select -expandproperty Access

    foreach ($permission in $permissions) {
        [string]$id = $permission.IdentityReference
        $SamAccountName = $id.Split('\')[1]
        $ADObject = Get-ADObject -Filter ('SamAccountName -eq "{0}"' -f $SamAccountName) }
        $Record.IdentityReference = $permission.IdentityReference.ToString()

        switch ($ADObject.ObjectClass) {
            'user' {
                $Record.Class = $ADObject.ObjectClass
                $Record.GrpMember = ""
                $objRecord = New-Object PSObject -property $Record
                $Table += $objrecord
            }
            'group' {
                $Record.Class = $ADObject.ObjectClass
                $members = Get-ADGroupMember $SamAccountName }

                foreach ($member in $members) {
                    $Record.GrpMember = $member.name
                    $objRecord = New-Object PSObject -property $Record
                    $Table += $objrecord
                }
            }
        }
    }
}
$Table | export-csv $targetFile -NoTypeInformation -Encoding UTF8

Returning a table like this when formatted