Functions/Add-AzureADUserManagerEmailAddressProperty.ps1

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
<#
.SYNOPSIS
    This function adds a ManagerEmailAddress property to a user in Azure AD.
.SYNOPSIS
    This function adds a ManagerEmailAddress property to a user in Azure AD.
    The property is only added if the user's Manager property is already defined.
    This function assumes that the connection to Azure AD has already been made.
#>

function Add-AzureADUserManagerEmailAddressProperty {
    [CmdletBinding(PositionalBinding=$true)]
    param (
        # The user in Azure AD.
        [Parameter(Mandatory=$true, ValueFromPipeline=$true)]
        [ValidateNotNull()]
        [PSCustomObject]$user
    )

    # Add the property if the user has a manager
    try {
        $manager = Get-AzureADUserManager -ObjectId $user.ObjectId -ErrorAction Stop
        if ($manager) {
            $user | Add-Member -NotePropertyName "ManagerEmailAddress" -NotePropertyValue $manager.UserPrincipalName -Force
        }
    }
    catch {
        # Do nothing
    }

    # Return the user
    return $user
}