• caglararli@hotmail.com
  • 05386281520

Problem, extraction Role of users from AzureAD by powersell [closed]

Çağlar Arlı      -    38 Views

Problem, extraction Role of users from AzureAD by powersell [closed]

I'm trying to export users with their Role from AzureAD with the PowerShell script bellow.

When I execute the script I get an Excel file with four columns: Username, email, role and Description. But the columns of Role and Description are empty, I have noted the roles and the description of the user. I don't know why. Can someone help me identify the issue?

#Importer le module AzureAD
#Import-Module AzureAD


#Connect-AzureAD

$users = Get-AzureADUser -All $true


$userRoles = @()

foreach ($user in $users) {

    $roles = Get-AzureADUserAppRoleAssignment -ObjectId $user.ObjectId

     if ($roles.Count -gt 0) {
        foreach ($role in $roles) {
          
            $roleDetails = Get-AzureADDirectoryRole | Where-Object {$_.ObjectId -eq $role.Id}
            $userRoles += [PSCustomObject]@{
                "UserName" = $user.DisplayName
                "Email"           = $user.UserPrincipalName
                "Role"            = $roleDetails.DisplayName
                "Description"     = $roleDetails.Description
            }
        }
    } else {
      
        $userRoles += [PSCustomObject]@{
            "UserName" = $user.DisplayName
            "Email"           = $user.UserPrincipalName
            "Role"            = "No Role"
            "Description"     = ""
        }
    }
}


$userRoles | Export-Excel -Path "C:\Users\user1\Documents\list.xlsx" -AutoSize