Functions/Convert-ActiveDirectoryUserToMSPCompleteUser.ps1

<#
.SYNOPSIS
    This function converts an Active Directory user to a MSPComplete user.
.DESCRIPTION
    This function converts an Active Directory user to a MSPComplete user.
    The conversion is done by mapping the Active Directory user's properties to the MSPComplete
    user's properties and extended properties.
#>

function Convert-ActiveDirectoryUserToMSPCompleteUser {
    [CmdletBinding(PositionalBinding=$true)]
    [OutputType([PSCustomObject])]
    param (
        # The Active Directory user
        [Parameter(Mandatory=$true, ValueFromPipeline=$true)]
        [ValidateNotNull()]
        [PSCustomObject]$User
    )

    # Create the MSPComplete user object
    $mspCompleteUser = [PSCustomObject]@{}

    # Retrieve the map from MSPComplete user properties to Active Directory user properties
    $propertyMap = Get-MSPCompleteUserToActiveDirectoryUserPropertyMap

    # Convert the properties from the Active Directory user to the MSPComplete user
    foreach ($property in $propertyMap.GetEnumerator()) {
        $mspCompleteUser | Add-Member -NotePropertyName $property.Key -NotePropertyValue "$($User.($property.Value))"
    }

    # Retrieve the map from MSPComplete user extended properties to Active Directory user properties
    $extendedPropertyMap = Get-MSPCompleteUserToActiveDirectoryUserExtendedPropertyMap

    # Convert the extended properties from the Active Directory user to the MSPComplete user
    $mspCompleteUser | Add-Member -NotePropertyName "ExtendedProperties" -NotePropertyValue @{}
    foreach ($property in $extendedPropertyMap.GetEnumerator()) {
        if (![String]::IsNullOrWhiteSpace($User.($property.Value))) {
            $mspCompleteUser.ExtendedProperties.Add($property.Key, "$($User.($property.Value))")
        }
    }

    # Set the PrimaryEmailAddress property to be the same as the UserPrincipalName property if is is
    # not set
    if ([String]::IsNullOrWhiteSpace($mspCompleteUser.PrimaryEmailAddress)) {
        $mspCompleteUser.PrimaryEmailAddress = $mspCompleteUser.UserPrincipalName
    }

    # Return the converted user
    return $mspCompleteUser
}