Created
July 26, 2020 23:40
-
-
Save tathamoddie/2385a3a63caefd7eed06426469eaa1ba to your computer and use it in GitHub Desktop.
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
[CmdletBinding()] | |
param | |
( | |
[Parameter()] | |
[String[]] | |
$StartingUsers, | |
[Parameter()] | |
[bool] | |
$EnabledUsersOnly = $true | |
) | |
$upnsToLookup = New-Object System.Collections.Stack | |
$StartingUsers | ` | |
ForEach-Object { | |
$upnsToLookup.Push($_) | |
} | |
$usersFound = New-Object System.Collections.Generic.List[PSObject] | |
$upnsToLookup | ` | |
ForEach-Object { | |
$user = Get-AzureADUser -SearchString $_ | |
$usersFound.Add($user) | |
} | |
do { | |
$nextLookup = $upnsToLookup.Pop() | |
$result = Get-AzureADUserDirectReport -ObjectId $nextLookup | ` | |
Where-Object { $_.UserType -eq 'Member' } | |
if ($EnabledUsersOnly) { | |
$result = $result | Where-Object { $_.AccountEnabled } | |
} | |
$result | ForEach-Object { | |
$upnsToLookup.Push($_.UserPrincipalName) | |
$usersFound.Add($_) | |
} | |
Write-Progress -Activity "$($upnsToLookup.Count) lookups in queue; $($usersFound.Count) users found" | |
} while ($upnsToLookup.Count -gt 0) | |
$usersFound | Sort-Object -Unique -Property UserPrincipalName |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment