Last active
August 8, 2018 16:02
-
-
Save JoeDibley/a6bf831b37e477fa31c341ca04f207d5 to your computer and use it in GitHub Desktop.
Gets AD group members from multiple domains and can also do recursive lookup. Uses string manipulation to get Domain and if fails uses GC
This file contains 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-GroupMembers | |
{ | |
param ( | |
[Parameter( | |
Position = 0, | |
ValueFromPipeline = $true, | |
ValueFromPipelineByPropertyName = $true, | |
ValueFromRemainingArguments = $false | |
)] | |
[ValidateNotNullOrEmpty()] | |
[Alias('Name', 'ADGroup')] | |
[string[]]$GroupName, | |
[Parameter()] | |
[Alias('Recursive', 'Indirect')] | |
[switch]$Nested | |
) | |
foreach ($Group in $GroupName) | |
{ | |
#Getting Domain Value for global catalog try if required | |
$Domain = Get-ADDomain | Select-Object -ExpandProperty Netbios | |
[array]$AllMembers = Get-ADGroup -Identity $Group -Properties * -Server "$Domain`:3268" | Select-Object -ExpandProperty Members | |
$a = 0 | |
foreach ($member in $AllMembers) | |
{ | |
Write-Progress -PercentComplete ($a / $AllMembers.count * 100 ) -Activity "Enumerating Group: $Group" -CurrentOperation $member | |
$a++ | |
$Split = $member -split ",DC=" | |
$Join = $Split[1 .. ($split.count)] -join "." -replace """", "" | |
$DN = $member -replace """", "" | |
#For some reason get-adobject sometimes errors. If thats the case try using global catalog | |
try | |
{ | |
$obj = Get-ADObject $DN -Property * -Server $Join -ErrorAction Stop | |
} | |
catch | |
{ | |
#Try using Get-ADUser and Get-ADGroup instead in a nested Try statement | |
try { $obj = Get-ADUser $DN -Properties * -Server "$Domain`:3268" -ErrorAction Stop } | |
Catch{ $obj = Get-ADGroup $DN -Properties * -Server "$Domain`:3268" -ErrorAction Stop } | |
} | |
if ($Nested) | |
{ | |
if ($obj.ObjectClass -eq "group") { Get-GroupMembers -Group $obj.DistinguishedName } | |
} | |
[System.Array] $Objs += $obj | |
} | |
} | |
Write-Progress -Completed -Activity "Completed Enumeration" | |
$Objs | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment