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

问题描述 投票:0回答:1

在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

相反,我希望它列出属于管理员/用户的所有用户及其对每个文件夹的权限并丢弃该组。

另外,如何将Convert-Path添加到选择路径语句中,以便显示的路径只是C:\ Test?

谢谢!

powershell acl
1个回答
0
投票

我无法通过链接帖子和/或PowerShell访问控制模块来解决它,仍然只有组。所以最后我能够得到我想要的信息,结合不同的其他有用的帖子,如:

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

© www.soinside.com 2019 - 2024. All rights reserved.