powershell - 使用 PowerShell 有效访问 Active Directory 对象

标签 powershell active-directory

我正在尝试获取某些 Active Directory 用户对各种 Active Directory 对象的有效权限。我可以从 UI 中看到这些权限 - enter image description here

我正在尝试使用 Powershell 获取此信息。我已经尝试过 dsaclsGet-Acls 但这些都没有提供有效的权限。 这两者都给出了“谁有访问/权限”,这与“谁有什么有效权限”不同。这些也没有列出提供有效访问上下文的所有详细信息。

任何关于如何以编程方式实现这一点的指示将不胜感激。

更新-

这里的有效权限是指现实中基于继承或其他不同级别设置的规则允许对象具有哪些权限。

例如-

下例中的所有属性对于 Get-ACL 都不可见。 enter image description here

另一个 Get-Acl 显示但 UI 不同的示例是,在解析 ObjectType 和 InheritedObjectType 中的值(使用提到的 get-effective 访问函数)后,我通过 Get-ACL 为其中一个 OU 提取域管理员权限由 Santiago Squarzon 撰写)我得到 -

enter image description here

虽然UI有效访问显示-

enter image description here

我的最终目标是使用 powershell 获得上面截图中的所有权限。

最佳答案

这与您正在寻找的非常接近。 Source更多细节。在 高级安全设置Get-ACL 的访问控制列表不如Effective Access 容易阅读> 我认为没有办法解决这个问题。我确实认为,一旦习惯了,Get-ACL 会在您知道要查找的内容时提供更多详细信息\过滤 ACL 以获得您要查找的内容。

代码

function Get-EffectiveAccess {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory, ValueFromPipeline, ValueFromPipelineByPropertyName)]
        [ValidatePattern('(?:(CN=([^,]*)),)?(?:((?:(?:CN|OU)=[^,]+,?)+),)?((?:DC=[^,]+,?)+)$')]
        [alias('DistinguishedName')]
        [string] $Identity,

        [parameter()]
        [alias('Domain')]
        [string] $Server
    )

    begin {
        $guid    = [guid]::Empty
        $GUIDMap = @{}

        if($PSBoundParameters.ContainsKey('Server')) {
            $domain = Get-ADRootDSE -Server $Server
        }
        else {
            $domain = Get-ADRootDSE
        }

        $params = @{
            SearchBase  = $domain.schemaNamingContext
            LDAPFilter  = '(schemaIDGUID=*)'
            Properties  = 'name', 'schemaIDGUID'
            ErrorAction = 'SilentlyContinue'
        }
        $adObjParams = @{
            Properties = 'nTSecurityDescriptor'
        }

        if($PSBoundParameters.ContainsKey('Server')) {
            $params['Server']  = $Server
            $adObjParams['Server'] = $Server
        }
        $schemaIDs = Get-ADObject @params

        $params['SearchBase'] = "CN=Extended-Rights,$($domain.configurationNamingContext)"
        $params['LDAPFilter'] = '(objectClass=controlAccessRight)'
        $params['Properties'] = 'name', 'rightsGUID'
        $extendedRigths = Get-ADObject @params

        foreach($i in $schemaIDs) {
            if(-not $GUIDMap.ContainsKey([guid] $i.schemaIDGUID)) {
                $GUIDMap.Add([guid] $i.schemaIDGUID, $i.name)
            }
        }
        foreach($i in $extendedRigths) {
            if(-not $GUIDMap.ContainsKey([guid] $i.rightsGUID)) {
                $GUIDMap.Add([guid] $i.rightsGUID, $i.name)
            }
        }
    }

    process {
        try {
            $adObjParams['Identity'] = $Identity
            $object = Get-ADObject @adObjParams

            foreach($acl in $object.nTSecurityDescriptor.Access) {
                if($guid.Equals($acl.ObjectType)) {
                    $objectType = 'All Objects (Full Control)'
                }
                elseif($GUIDMap.ContainsKey($acl.ObjectType)) {
                    $objectType = $GUIDMap[$acl.ObjectType]
                }
                else {
                    $objectType = $acl.ObjectType
                }

                if($guid.Equals($acl.InheritedObjectType)) {
                    $inheritedObjType = 'Applied to Any Inherited Object'
                }
                elseif($GUIDMap.ContainsKey($acl.InheritedObjectType)) {
                    $inheritedObjType = $GUIDMap[$acl.InheritedObjectType]
                }
                else {
                    $inheritedObjType = $acl.InheritedObjectType
                }

                [PSCustomObject]@{
                    Name                  = $object.Name
                    IdentityReference     = $acl.IdentityReference
                    AccessControlType     = $acl.AccessControlType
                    ActiveDirectoryRights = $acl.ActiveDirectoryRights
                    ObjectType            = $objectType
                    InheritedObjectType   = $inheritedObjType
                    InheritanceType       = $acl.InheritanceType
                    IsInherited           = $acl.IsInherited
                }
            }
        }
        catch {
            $PSCmdlet.WriteError($_)
        }
    }
}

例子

  • 获取名为 ExampleOU 的组织单位的有效访问权限:
Get-ADOrganizationalUnit -Filter "Name -eq 'ExampleOU'" |
    Get-EffectiveAccess | Out-GridView
  • 获取受信任域上名为 ExampleOU 的组织单位的有效访问:
Get-ADOrganizationalUnit -Filter "Name -eq 'ExampleOU'" -Server trustedDomain |
    Get-EffectiveAccess -Server trustedDomain | Out-GridView
  • 与上面相同,但使用 OU 的 DistinguishedName 属性:
Get-EffectiveAccess -Identity 'OU=ExampleOU,DC=domainName,DC=com' | Out-GridView
  • 将名为exampleGroup 的组的Effective Access 存储在一个变量中:
$effectiveAccess = Get-ADGroup exampleGroup | Get-EffectiveAccess
  • 获取在域中找到的前 10 个 OU 的有效访问:
Get-ADOrganizationalUnit -Filter * | Select -First 10 |
    Get-EffectiveAccess | Out-GridView

示例

作为引用,这是 Full Control 使用 Get-ACL 的样子

fullcontrol

BUILTIN\Administrators 相比,BUILTIN\Administrators 对此 OU 具有写入权限但没有完全控制

writepermissions

关于powershell - 使用 PowerShell 有效访问 Active Directory 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67696850/

相关文章:

java - 仅检索 Active Directory 上启用的帐户

java - LDAP 服务器更新和事件通知

java - Powershell 比较数组以检查 GUID

powershell - Azure Add-AzureDisk 不工作

java - 如何将自定义权限填充器与 Spring Security 和 ActiveDirectoryLdapAuthenticationProvider 一起使用?

hadoop - 具有LDAP认证的Cloudera Manager

c# - 为什么无法为远程 RunspacePool 传递 InitialSessionState?

powershell - 是否可以以某种方式将 WindowsIdentity 类型转换或转换为 PSCredential 类型?

windows - 如何使用 Ansible 将自定义脚本扩展添加到 Azure VM

delphi - 使用 LDAP 或 ADSI 与 Delphi 进行用户帐户管理