Public/Person/Add-PopuliPersonEmail.ps1

function Add-PopuliPersonEmail
{

    [CmdletBinding(DefaultParametersetName='PersonId', PositionalBinding=$true)]
    param
    (
        [Parameter(ParameterSetName='PersonId', Mandatory=$true)][string]$PersonId,

        [Parameter(ParameterSetName='OrganizationId', Mandatory=$true)][string]$OrganizationId,

        [Parameter(Mandatory=$true)][ValidatePattern('.*@.*')][string]$EmailAddress,

        [Parameter(Mandatory=$true)][ValidateSet('HOME','WORK','SCHOOL','OTHER')][string]$Type,

        [Parameter(Mandatory=$false)][switch]$Primary = $false,

        [Parameter(Mandatory=$false)][switch]$Public = $true,

        [Parameter(Mandatory=$false)][ValidateLength(200,999)][string]$PopuliToken = $env:PopuliToken,
        [Parameter(Mandatory=$false)][ValidatePattern('^(?i)https:\/\/\S+\.populiweb\.com\/?$')][string]$BaseUrl = $env:PopuliBaseUrl
    )


    try
    {

        if ($BaseUrl -match '\/$')
        {
            $BaseUrl = $BaseUrl -replace '\/$'
        }

        $header = @{
            Authorization = $PopuliToken
        }

        $body = @{
            task = 'addEmailAddress'
            email_address = $EmailAddress
            type = $Type
            primary = $Primary
            public = $Public
        }

        if ($PSBoundParameters.Keys -contains 'PersonId')
        {
            $body += @{person_id = $PersonId}
        }
        elseif ($PSBoundParameters.Keys -contains 'OrganizationId')
        {
            $body += @{organization_id = $OrganizationId}
        }
        else 
        {
            throw "-PersonId or -OrganizationId must be specified"
        }


        $response = Invoke-RestMethod -Uri "$BaseUrl/api/" -Method POST -ContentType 'application/x-www-form-urlencoded' -Body $body -Headers $header

        $addEmailResp = $response.response

        Write-Log "Adding email ""$EmailAddress"" to ""$PersonId$OrganizationId"". Response: ""$($addEmailResp.Id)""" -PersonId $PersonId

        return $addEmailResp
    }
    catch
    {
        Write-Log "Unhandled exception" -LogType: error -ErrorObject $_ -PersonId $PersonId
        Write-Error $_

        return $null
    }


}