Functions/Convert-GSuiteUserToMSPCompleteUser.ps1

<#
.SYNOPSIS
    This function converts a GSuite user to a MSPComplete user.
.DESCRIPTION
    This function converts a GSuite user to a MSPComplete user.
    The conversion is accomplished by mapping the GSuite user's properties and extended
    properties to their corresponding MSPComplete properties.
#>

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

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

    # Retrieve the mapping from MSPComplete user to GSuite user properties
    $propertyMap = Get-MSPCompleteUserToGSuiteUserPropertyMap

    # Add all properties to the MSPComplete user
    foreach ($property in $propertyMap.GetEnumerator()) {
        # Invoke-Expression is used, because some values are more than one layer under the user object
        # Example : $user.name.fullName
        $value = Invoke-Expression "`$user.$($property.Value)"
        if (![String]::IsNullOrWhiteSpace($value)) {
            $mspCompleteUser | Add-Member -NotePropertyName $property.Name -NotePropertyValue $value
        }
    }

    # Retrieve the map from MSPComplete user extended properties to GSuite user properties
    $extendedPropertyMap = Get-MSPCompleteUserToGSuiteUserExtendedPropertyMap

    # Convert the extended properties to the MSPComplete user
    $mspCompleteUser | Add-Member -NotePropertyName "ExtendedProperties" -NotePropertyValue @{ } -Force

    # Add extended properties to the MSPComplete user
    foreach ($property in $extendedPropertyMap.GetEnumerator()) {
        # Invoke-Expression is used, because some values are more than one layer under the user object
        # Example : $user.organizations.name
        $value = Invoke-Expression "`$user.$($property.Value)"
        if (![String]::IsNullOrWhiteSpace($value)) {
            $mspCompleteUser.ExtendedProperties.Add($property.Name, $value)
        }
    }

    # Add manager email address
    $manager = $user.relations | Where-Object { $_.type -eq "manager" }
    if (![String]::IsNullOrWhiteSpace($manager.value)) {
        $mspCompleteUser.ExtendedProperties.Add("ManagerEmailAddress", $manager.value)
    }

    # Get the home address and add properties
    $homeAddress = $user.addresses | Where-Object { $_.type -eq "home" }
    $addressPropertyMap = @{
        AddressLine1    = "streetAddress"
        PostalOrZipCode = "postalCode"
        CountryOrRegion = "region"
    }
    foreach ($property in $addressPropertyMap.GetEnumerator()) {
        if (![String]::IsNullOrWhiteSpace($homeAddress.($property.Value))) {
            $mspCompleteUser.ExtendedProperties.Add($property.Name, $homeAddress.($property.Value))
            $mspCompleteUser | Add-Member -NotePropertyName $property.Name -NotePropertyValue $homeAddress.($property.Value)
        }
    }

    # Add phone numbers
    $landLinePhone = $user.phones | Where-Object { $_.type -eq "home" }
    if (![String]::IsNullOrWhiteSpace($landLinePhone.value)) {
        $mspCompleteUser.ExtendedProperties.Add("TelephoneNumber", $landLinePhone.value)
    }
    $mobilePhone = $user.phones | Where-Object { $_.type -eq "mobile" }
    if (![String]::IsNullOrWhiteSpace($mobilePhone.value)) {
        $mspCompleteUser.ExtendedProperties.Add("MobilePhoneNumber", $mobilePhone.value)
    }

    # Return the converted user
    return $mspCompleteUser
}