Functions/Converters/ConvertTo-CdsObject.ps1

<#
    .SYNOPSIS
    Transform Entity to custom object.
#>

function ConvertTo-CdsObject {
    [CmdletBinding()]
    [OutputType([PsObject])]
    param
    (
        [Parameter(Mandatory, ValueFromPipeline)]
        [Microsoft.Xrm.Sdk.Entity]
        $Record
    )
    begin {  
        $StopWatch = [System.Diagnostics.Stopwatch]::StartNew(); 
        Trace-CdsFunction -Name $MyInvocation.MyCommand.Name -Stage Start -Parameters ($MyInvocation.MyCommand.Parameters); 
    }    
    process {               
        $hash = @{};
        $hash["Id"] = $Record.Id;
        $hash["LogicalName"] = $Record.LogicalName;
        $hash["Record"] = $Record;
        foreach($attribute in $Record.Attributes)
        {
            $value = "";
            if($Record.FormattedValues.ContainsKey($attribute.Key))
            {
                $value = $Record.FormattedValues[$attribute.Key];
                $hash["$($attribute.Key)_Value"] = $Record[$attribute.Key];
            }
            else
            {
                $value = $Record[$attribute.Key];
            }

            $hash[$attribute.Key] = $value;
        }

        $object = New-Object PsObject -Property $hash;
        $object;
    }
    end {
        $StopWatch.Stop();
        Trace-CdsFunction -Name $MyInvocation.MyCommand.Name -Stage Stop -StopWatch $StopWatch;
    }
}

Export-ModuleMember -Function ConvertTo-CdsObject -Alias *;