Search code examples
powershellazure-active-directory

Trouble with extension attributes when pulling AAD user data in powershell


I'm trying to pull a detailed user report that my company can use for billing purposes with our clients. I am able to pull and gather most properties for each user to a csv file, but I am having trouble when it comes to Extension Attributes. In following the examples found here, I wasn't able to make any progress. I'm hoping someone on here is knowledgeable and willing to walk me through what I'm missing. The code and error are below.

Connect-AzureAD -Credential $credential

# Get all Azure AD users
$AzADUsers = Get-AzureADUser -All $true | Select-Object -Property *


# Display progress bar
$progressCount = 0
for ($i = 0; $i -le $AzADUsers.Count; $i++) {

    Write-Progress `
        -Id 0 `
        -Activity "Retrieving User " `
        -Status "$progressCount of $($AzADUsers.Count)" `
        -PercentComplete (($progressCount / $AzADUsers.Count) * 100)
    $progressCount++
}

$UserId = (Get-AzureADUser -Searchstring $_.UserPrincipalName).ObjectId
    Get-AzureADUser -ObjectId $UserId | Select -ExpandProperty ExtensionProperty
    pause

Gives me this error

Get-AzureADUser : Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'ObjectId'. Specified method is not supported. At C:\Users\James\OneDrive - Veeya\Desktop\O365 User License Reporting and Management\adcsv.ps1:22 char:28 + Get-AzureADUser -ObjectId $UserId | Select -ExpandProperty Extens ...

  •                           ~~~~~~~
    
    • CategoryInfo : InvalidArgument: (:) [Get-AzureADUser], ParameterBindingException
    • FullyQualifiedErrorId : CannotConvertArgument,Microsoft.Open.AzureAD16.PowerShell.GetUser

Solution

  • I tried in my environment and got same error:

    Console: enter image description here

    Command:

    I tried the below commands which is executed with Extension property successfully.

    Connect-AzureAD
    
    # Get all Azure AD users
    $AzADUsers = Get-AzureADUser
    
    foreach($user in $AzADUsers)
    {
        write-host "User Name :  " $user.DisplayName
        $UserId = (Get-AzureADUser -ObjectId  $user.UserPrincipalName).ObjectId
       Get-AzureADUser -ObjectId $UserId | Select -ExpandProperty ExtensionProperty
       write-host " "
    }
    

    Console:

    enter image description here

    enter image description here

    Sample output for single user:

    Get-AzureADUser -ObjectId "< Object Id of user >" | Select -ExpandProperty ExtensionProperty
    
    Key                                                         Value                                                                              
    ---                                                         -----                                                                              
    odata.metadata                                              https://graph.windows.net/<>/$metadata#directo...
    odata.type                                                  Microsoft.DirectoryServices.User                                                   
    createdDateTime                                             04-01-2023 06:46:26                                                                
    employeeId                                                                                                                                     
    onPremisesDistinguishedName                                                                                                                    
    userIdentities                                              []                                                                                 
    extension_411567d7bfd94d2eb0f82xxxxxx_YourPropertyName YourPropertyValue   
    

    Reference: Azure AD cmdlets to work with extension attributes | Microsoft Learn.