Last active
September 17, 2021 06:23
-
-
Save hansgafriedzal/c09557aa471ce16b0151516d937ead9d to your computer and use it in GitHub Desktop.
Get MSOL users and their AAD directory roles.
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-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