Skip to content

Instantly share code, notes, and snippets.

@tathamoddie
Created July 26, 2020 23:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tathamoddie/2385a3a63caefd7eed06426469eaa1ba to your computer and use it in GitHub Desktop.
Save tathamoddie/2385a3a63caefd7eed06426469eaa1ba to your computer and use it in GitHub Desktop.
[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