Skip to content

Instantly share code, notes, and snippets.

@hansgafriedzal
Last active September 17, 2021 06:23
Show Gist options
  • Save hansgafriedzal/c09557aa471ce16b0151516d937ead9d to your computer and use it in GitHub Desktop.
Save hansgafriedzal/c09557aa471ce16b0151516d937ead9d to your computer and use it in GitHub Desktop.
Get MSOL users and their AAD directory roles.
function Get-AzureADUserDirectoryRole {
param
(
[Parameter(ValueFromPipeline = $true)]
[Microsoft.Online.Administration.User] $User
)
BEGIN {
$_directoryRoles = Get-AzureADDirectoryRole
$_directoryRoleMembers = $_directoryRoles | %{
[PSCustomObject] @{
DirectoryRoleName = $_.DisplayName
DirectoryRoleMembers = Get-AzureADDirectoryRoleMember -ObjectId $_.ObjectId | %{
$_.UserPrincipalName
}
}
} | ?{$_.DirectoryRoleMembers}
}
PROCESS {
$_directoryRoleMembers `
| ?{$_.DirectoryRoleMembers -contains $User.UserPrincipalName} `
| %{
return [PSCustomObject] @{
UserPrincipalName = $User.UserPrincipalName
DirectoryRoles = $_.DirectoryRoleName
}
}
}
}
# USAGE
$timestamp = Get-Date -format "yyyyMMddHHmmss"
Get-MsolUser -All `
| Get-AzureADUserDirectoryRole `
| Export-Csv "aadroles_$($timestamp).csv" -NoTypeInformation
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment