Functions/Plugins/Get-CdsPluginTraces.ps1

<#
    .SYNOPSIS
    Retrieve plugin traces
#>

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

        [Parameter(Mandatory=$false)]
        [bool]
        $ErrorsOnly = $false,

        [Parameter(Mandatory=$false)]
        [int]
        $Take = 50
    )
    begin {
        $StopWatch = [System.Diagnostics.Stopwatch]::StartNew();
        Trace-CdsFunction -Name $MyInvocation.MyCommand.Name -Stage Start -Parameters ($MyInvocation.MyCommand.Parameters);       
    }    
    process {
       
        $queryTraces = New-CdsQueryExpression -LogicalName "plugintracelog" -Columns *;
        $queryTraces = $queryTraces | Add-CdsQueryOrder -Field "performanceexecutionstarttime" -OrderType Descending;
        if($ErrorsOnly)
        {
            $queryTraces = $queryTraces | PowerCds\Add-CdsQueryCondition -Field "exceptiondetails" -Condition NotNull;
        }
        $queryTraces.TopCount = $Take;
        $traces = $CdsClient | Get-CdsMultipleRecords -Query $queryTraces;

        $selectedTraces = $traces | Select-Object "performanceexecutionstarttime", "performanceexecutionduration", "operationtype", "typename", "correlationid", "depth", "mode", "messagename", "primaryentity", "plugintracelogid"  | Out-GridView -OutputMode Multiple;

        $selectedTraces | ForEach-Object {
            $fullTrace = $traces | Where-Object -Property "plugintracelogid" -EQ -Value $_.plugintracelogid;
            $fullTrace;
        }
    }
    end {
        $StopWatch.Stop();
        Trace-CdsFunction -Name $MyInvocation.MyCommand.Name -Stage Stop -StopWatch $StopWatch;
    }    
}

Export-ModuleMember -Function Get-CdsPluginTraces -Alias *;