Retrieving a list of all Azure AD role assignments sounds easy enough, right? Well, there are some things to consider, here is waht.

Table of Contents

  1. Introduction
  2. Script

Introduction

Unfortunately, its not straight forward, to get list of all Azure AD role assignments, unless you are not using Privileged Identity Management (PIM).
First, we need the Microsoft Graph PowerShell SDK. Follow these steps.
Currently, to retrieve eligible, its required to set the Microsoft Graph profile to beta. Also, those information can only be queried using the Windpws PowerShell.

Script

The gist can either be found here or explained in detail below.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
Connect-MgGraph -Scopes RoleEligibilitySchedule.Read.Directory, RoleAssignmentSchedule.Read.Directory, CrossTenantInformation.ReadBasic.All, AuditLog.Read.All, User.Read.All
Select-MgProfile -Name Beta

# get all user to resolve IDs
$users = Get-MgUser -All
# get all groups to resolve IDs
$groups = Get-MgGroup -All

# get all Azure AD role definitions to resolve IDs
$roles = Get-MgRoleManagementDirectoryRoleDefinition

# get all role assignments
$eligible_role_assignments = Get-MgRoleManagementDirectoryRoleEligibilitySchedule -ExpandProperty "*" -All:$true
$assigned_role_assignments = Get-MgRoleManagementDirectoryRoleAssignmentScheduleInstance -ExpandProperty "*" -All:$true

[System.Collections.ArrayList]$resolved_assignments = @()

foreach ($assignment in $eligible_role_assignments) {
$user = $users | Where-Object { $_.id -eq $assignment.PrincipalId }
$group = $groups | Where-Object { $_.id -eq $assignment.PrincipalId }

$obj = [pscustomobject]@{
'role' = $roles | Where-Object { $_.id -eq $assignment.RoleDefinitionId } | Select-Object -ExpandProperty DisplayName
'user' = $user | Select-Object -ExpandProperty UserPrincipalName
'group' = $group | Select-Object -ExpandProperty DisplayName
'user_enabled' = $user | Select-Object -ExpandProperty AccountEnabled
}

$resolved_assignments.Add($obj) | Out-Null
}

foreach ($assignment in $assigned_role_assignments) {
$user = $users | Where-Object { $_.id -eq $assignment.PrincipalId }
$group = $groups | Where-Object { $_.id -eq $assignment.PrincipalId }

$obj = [pscustomobject]@{
'role' = $roles | Where-Object { $_.id -eq $assignment.RoleDefinitionId } | Select-Object -ExpandProperty DisplayName
'user' = $user | Select-Object -ExpandProperty UserPrincipalName
'group' = $group | Select-Object -ExpandProperty DisplayName
'user_enabled' = $user | Select-Object -ExpandProperty AccountEnabled
}

$resolved_assignments.Add($obj) | Out-Null
}

Write-Output $resolved_assignments

I hope this makes your life a little simpler 😉