Functions/Entity/New-CdsEntity.ps1

<#
    .SYNOPSIS
    Initialize Entity object instance.
#>

function New-CdsEntity {
    [CmdletBinding()]
    [OutputType("Microsoft.Xrm.Sdk.Entity")]
    param
    (        
        [Parameter(Mandatory = $true)]
        [ValidateNotNullOrEmpty()]   
        [String]
        $LogicalName,

        [Parameter(Mandatory = $false)]
        [ValidateNotNullOrEmpty()]   
        [Guid]
        $Id,

        [Parameter(Mandatory = $false)]
        [Hashtable]
        $Attributes
    )
    begin {   
        $StopWatch = [System.Diagnostics.Stopwatch]::StartNew(); 
        Trace-CdsFunction -Name $MyInvocation.MyCommand.Name -Stage Start -Parameters ($MyInvocation.MyCommand.Parameters); 
    }    
    process {
        $record = New-Object -TypeName "Microsoft.Xrm.Sdk.Entity" -ArgumentList $LogicalName;
        if($PSBoundParameters.ContainsKey('Id')) {
            $record.Id = $Id;
        }

        if($PSBoundParameters.ContainsKey('Attributes')) {
            $Attributes.Keys | ForEach-Object {
                $key = $_;
                $value = $Attributes[$key];
                $record | Set-CdsAttributeValue -Name $key -Value $value | Out-Null;
            };
        }

        $record;
    }
    end {
        $StopWatch.Stop();
        Trace-CdsFunction -Name $MyInvocation.MyCommand.Name -Stage Stop -StopWatch $StopWatch;
    }    
}

Export-ModuleMember -Function New-CdsEntity -Alias *;

Register-ArgumentCompleter -CommandName New-CdsEntity -ParameterName "LogicalName" -ScriptBlock {

    param($CommandName, $ParameterName, $WordToComplete, $CommandAst, $FakeBoundParameters)

    $validLogicalNames = Get-CdsEntitiesLogicalName;
    return $validLogicalNames | Where-Object { $_ -like "$wordToComplete*"};
}