Created
April 13, 2020 17:43
-
-
Save PshMike/f7fa112d0b10171a4deb17bae5197c6f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Function Get-DeepADMembership | |
{ | |
[cmdletbinding()] | |
param ( | |
[Parameter(Mandatory, | |
ParameterSetName = 'ByDN')] | |
[string[]]$DN, | |
[Parameter(Mandatory, | |
ParameterSetName = 'ByUser')] | |
[string[]]$User, | |
[string]$Server = $env:userdnsdomain , | |
[string[]]$ProcessedDNs = @() | |
) | |
begin | |
{ | |
$output = @() | |
$GC = $Server + ":3268" | |
switch ( $pscmdlet.ParameterSetName ) | |
{ | |
'ByDN' | |
{ | |
$StartDN = $DN | |
} | |
'ByUser' | |
{ | |
$StartDN = $User | get-aduser -Properties memberOf -Server $GC | select-object -expandproperty memberOf | |
} | |
} | |
} | |
process | |
{ | |
foreach ( $d in $StartDN ) | |
{ | |
$output += @(Get-ADObject $d -Properties samAccountName -Server $GC ) | |
$ProcessedDNs += $d | |
$result = @(Get-ADObject -Identity $d -Properties memberof -Server $GC | Select-Object -ExpandProperty memberOf ) | |
if ( $result.Count ) | |
{ | |
foreach ($r in $result) | |
{ | |
if ($r -notin $ProcessedDNs) | |
{ | |
$output += @(Get-DeepADMembership -DN $r -ProcessedDNs $d -Server $Server) | |
} | |
} | |
} | |
} | |
} | |
end | |
{ | |
$output | Select-Object -Unique | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment