|
| 1 | +function Get-ADUserTransitiveGroupMembership { |
| 2 | + <# |
| 3 | +.SYNOPSIS |
| 4 | +Get the full transitive group membership of an Active Directory user. |
| 5 | +
|
| 6 | +.DESCRIPTION |
| 7 | +Get the full transitive group membership of an Active Directory user by searching the global catalog. This performs a |
| 8 | +transitive LDAP query which effectively flattens the group membership hierarchy more efficiently than a recursive memberOf |
| 9 | +lookup. |
| 10 | +
|
| 11 | +.PARAMETER UserDN |
| 12 | +The distinguished name of the user to search for. This is required, and it accepts pipeline input. |
| 13 | +
|
| 14 | +.PARAMETER Server |
| 15 | +A global catalog domain controller to connect to. This will find a GC automatically if none is specified. |
| 16 | +
|
| 17 | +.PARAMETER Port |
| 18 | +Port to connect to the global catalog service on. Defaults to 3268. |
| 19 | +
|
| 20 | +.EXAMPLE |
| 21 | +Get-ADUser -Identity JaneDoe | Select-Object -ExpandProperty DistinguishedName | Get-NestedGroupMembership |
| 22 | +
|
| 23 | +Gets the transitive group membership of the user JaneDoe, including all nested group memberships. |
| 24 | +
|
| 25 | +.NOTES |
| 26 | +Author: Sam Erde |
| 27 | +Company: Sentinel Technologies, Inc |
| 28 | +Version: 1.0.0 |
| 29 | +Date: 2025-02-26 |
| 30 | +#> |
| 31 | + |
| 32 | + [CmdletBinding()] |
| 33 | + param ( |
| 34 | + [Parameter(Mandatory, ValueFromPipeline, HelpMessage = 'The distinguished name of the user to search for.')] |
| 35 | + [string]$UserDN, |
| 36 | + |
| 37 | + [Parameter(HelpMessage = 'A global catalog domain controller to connect to.')] |
| 38 | + [ValidateScript({ Test-Connection -ComputerName $_ -Count 1 -ErrorAction SilentlyContinue })] |
| 39 | + #[string]$Server = (Get-ADDomainController -Discover -Service GlobalCatalog).HostName, |
| 40 | + [string]$Server = ([System.DirectoryServices.ActiveDirectory.GlobalCatalog]::FindOne([System.DirectoryServices.ActiveDirectory.DirectoryContextType]::Forest)).Name, |
| 41 | + |
| 42 | + # Port to connect to the global catalog service on. |
| 43 | + [Parameter(HelpMessage = 'Port to connect to the global catalog service on. Default is 3268, or 3269 for using TLS.')] |
| 44 | + [ValidateSet(3268, 3269)] |
| 45 | + [int]$Port = 3268 |
| 46 | + ) |
| 47 | + |
| 48 | + process { |
| 49 | + # Set the searcher parameters |
| 50 | + $filter = "(&(objectClass=group)(member:1.2.840.113556.1.4.1941:=$UserDN))" |
| 51 | + $searcher = New-Object System.DirectoryServices.DirectorySearcher |
| 52 | + $searcher.SearchRoot = New-Object System.DirectoryServices.DirectoryEntry("LDAP://$Server`:$Port") |
| 53 | + $searcher.Filter = $filter |
| 54 | + $searcher.PageSize = 1000 |
| 55 | + |
| 56 | + # Properties to include in the results: |
| 57 | + $searcher.PropertiesToLoad.Add('name') | Out-Null |
| 58 | + $searcher.PropertiesToLoad.Add('distinguishedName') | Out-Null |
| 59 | + $searcher.PropertiesToLoad.Add('securityIdentifier') | Out-Null |
| 60 | + $searcher.PropertiesToLoad.Add('objectSid') | Out-Null |
| 61 | + |
| 62 | + $results = $searcher.FindAll() |
| 63 | + |
| 64 | + Write-Verbose "Found $($results.Count) groups for ${UserDN}." |
| 65 | + |
| 66 | + foreach ($result in $results) { |
| 67 | + [PSCustomObject]@{ |
| 68 | + GroupName = $result.Properties['name'][0] |
| 69 | + DistinguishedName = $result.Properties['distinguishedName'][0] |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | +} |
0 commit comments