Functions/Entity/Get-CdsAttributeValue.ps1

<#
    .SYNOPSIS
    Read entity attribute.
#>

function Get-CdsAttributeValue {
    [CmdletBinding()]
    param
    (        
        [Parameter(Mandatory = $true, ValueFromPipeline)]
        [ValidateNotNull()]
        [Microsoft.Xrm.Sdk.Entity]
        $Record,

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

        if(-not $Record.Contains($Name))
        {
            Write-HostAndLog -Message "Attribute '$Name' is not in given record." -Level WARN;
            return $null;
        }

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

Set-Alias GetAttributeValue Get-CdsAttributeValue;
Export-ModuleMember -Function Get-CdsAttributeValue -Alias *;

Register-ArgumentCompleter -CommandName Get-CdsAttributeValue -ParameterName "Name" -ScriptBlock {

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

    $record = $null;
    if (-not ($FakeBoundParameters.ContainsKey("Record")))
    {
        # TODO : Search record for logicalname in Pipeline
        return @();
    }
    else {
        $record = $FakeBoundParameters.Record;         
    }

    $validAttributeNames = @($record.Attributes.Keys);
    return $validAttributeNames | Where-Object { $_ -like "*$wordToComplete*"};
}