@Tristano,G,Giuseppe,JBP12 R Thanks for your patience on this.
You can leverage the below PowerShell scripts to pull the role definitions under a particular subscription of type group and followed by users inside those groups.
- If you want to pull the user under a specific group, use the below script.
Connect-AzAccount
#Set a particular subscription as context to run below role assignment cmdlet
Set-AzContext -SubscriptionId "<SubscriptionId>"
# Log in to AzureAD
Connect-AzureAD
# Get all role assignments
$roleAssignments = Get-AzRoleAssignment -ObjectId "<GroupObjectId>"
# Get all users under the AD Group
Get-AzADGroupMember -GroupObjectId $roleAssignments.ObjectId
- If you want to pull for all group and their memberships use the below.
# Log in to Azure
Connect-AzAccount
#Set a particular subscription as context to run below role assignment cmdlet
Set-AzContext -SubscriptionId "<SubscriptionId>"
# Log in to AzureAD
Connect-AzureAD
$roleAssignments = Get-AzRoleAssignment | Where-Object ObjectType -EQ "Group"
$output = foreach($assignment in $roleAssignments){
$users= Get-AzADGroupMember -GroupObjectId $assignment.ObjectId | Select-Object DisplayName, UserPrincipalName
foreach($user in $users){
$roleDefinitionName = (Get-AzRoleDefinition -Id $assignment.RoleDefinitionId).Name
$groupName = (Get-AzADGroup -ObjectId $assignment.ObjectId).DisplayName
[PSCustomObject]@{
RoleDefinitionName = $roleDefinitionName
GroupName = $groupName
Users = $user.DisplayName
Upn =$user.UserPrincipalName
}
}
}
$output | Export-Csv -Path "C:\Output.csv" -NoTypeInformation
Here is the sample screen shot output of above script for your reference:
Note: I have tested the above script in my local machine, and it is working fine. I would suggest you make changes based on your requirement and test it accordingly.
Hope this helps, let me know if you have any further questions on this.
Please accept as "Yes" if the answer is helpful so that it can help others in the community.