Functions/Convert-ExchangeOnlineUserToMSPCompleteUser.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<#
.SYNOPSIS
    This function converts an Exchange Online user to a MSPComplete user.
.DESCRIPTION
    This function converts an Exchange Online user to a MSPComplete user.
    The conversion is accomplished by mapping the Exchange Online user's properties and extended
    properties to their corresponding MSPComplete properties.
#>

function Convert-ExchangeOnlineUserToMSPCompleteUser {
    [CmdletBinding(PositionalBinding=$true)]
    [OutputType([PSObject])]
    param (
        # The Exchange Online User.
        [Parameter(Mandatory=$true, ValueFromPipeline=$true)]
        [ValidateNotNull()]
        $user
    )

    # Retrieve the mapping from MSPComplete user to Exchange Online user properties
    $propertyMap = Get-MSPCompleteUserToExchangeOnlineUserPropertyMap

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

    # Add all properties to the MSPComplete user
    foreach ($property in $propertyMap.GetEnumerator()) {
        if (![String]::IsNullOrWhiteSpace($user.($property.Value))) {
            $mspCompleteUser | Add-Member -NotePropertyName $property.Key -NotePropertyValue $user.($property.Value)
        }
    }

    # Retrieve the map from MSPComplete user extended properties to Exchange Online user properties
    $extendedPropertyMap = Get-MSPCompleteUserToExchangeOnlineUserExtendedPropertyMap

    # Convert the extended properties 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))
        }
    }

    # Return the converted user
    return $mspCompleteUser
}