Functions/Client/Upsert-CdsRecord.ps1

<#
    .SYNOPSIS
    Upsert entity record in CDS.
#>

function Upsert-CdsRecord {
    [CmdletBinding()]
    [OutputType([Guid])]
    param
    (    
        [Parameter(Mandatory=$false, ValueFromPipeline)]
        [Microsoft.Xrm.Tooling.Connector.CrmServiceClient]
        $CdsClient = $Global:CdsClient,

        [Parameter(Mandatory=$true, ValueFromPipeline)]
        [Microsoft.Xrm.Sdk.Entity]
        $Record,

        [Parameter(Mandatory=$false)]
        [switch]
        $BypassCustomPluginExecution = $false
    )
    begin {   
        $StopWatch = [System.Diagnostics.Stopwatch]::StartNew(); 
        Trace-CdsFunction -Name $MyInvocation.MyCommand.Name -Stage Start -Parameters ($MyInvocation.MyCommand.Parameters);
    }    
    process {

        $request = New-CdsRequest -Name "Upsert";
        $request | Add-CdsRequestParameter -Name "Target" -Value $Record | Out-Null;
        if($BypassCustomPluginExecution)
        {
            $request | Add-CdsRequestParameter -Name "BypassCustomPluginExecution" -Value $true | Out-Null;
        }

        $response = Invoke-CdsRequest -CdsClient $CdsClient -Request $request;
        $id = $response.Results["id"];
        $id;
    }
    end {
        $StopWatch.Stop();
        Trace-CdsFunction -Name $MyInvocation.MyCommand.Name -Stage Stop -StopWatch $StopWatch;
    }    
}

Export-ModuleMember -Function Upsert-CdsRecord -Alias *;