Extraction of IAM roles in Azure via script

Tristano,G,Giuseppe,JBP12 R 111 Reputation points
2024-10-17T13:07:02.9233333+00:00

Hello All,

is there a way to get all Azure IAM role assignment with also the details of the groups?

I explain better: if I go to the Azure portal and click download role assignment I get a list of users and groups with the pertaining role.

I would like to have in the same file the users contained in the group.

Could you please advise

giuseppe

Windows for business | Windows Server | User experience | PowerShell
Microsoft Security | Microsoft Entra | Other
{count} votes

Answer accepted by question author
  1. VenkateshDodda-MSFT 25,241 Reputation points Microsoft Employee Moderator
    2024-10-18T05:30:19.3433333+00:00

    @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:

    enter image description here

    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.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.