src/Query/New-XrmFetchExpression.ps1

<#
    .SYNOPSIS
    Return a fetch expression from fetch xml

    .DESCRIPTION
    Initialize new fetch expression object.

    .PARAMETER FetchXml
    FetchXML query string.

    .OUTPUTS
    Microsoft.Xrm.Sdk.Query.FetchExpression. The query, to pass to Get-XrmMultipleRecords.

    .EXAMPLE
    $xrmClient = New-XrmClient -ConnectionString $connectionString;
    $fetch = New-XrmFetchExpression -FetchXml '<fetch><entity name="contact"><attribute name="fullname" /><attribute name="emailaddress1" /></entity></fetch>';
    $contacts = $xrmClient | Get-XrmMultipleRecords -Query $fetch;

    .LINK
    https://github.com/AymericM78/PowerDataOps/blob/main/documentation/commands/New-XrmFetchExpression.md
#>

function New-XrmFetchExpression {
    [CmdletBinding()]
    [OutputType("Microsoft.Xrm.Sdk.Query.FetchExpression")]
    param
    (
        [Parameter(Mandatory = $true)]
        [ValidateNotNullOrEmpty()]   
        [String]
        $FetchXml
    )
    begin {  
        $StopWatch = [System.Diagnostics.Stopwatch]::StartNew(); 
        Trace-XrmFunction -Name $MyInvocation.MyCommand.Name -Stage Start -Parameters ($MyInvocation.MyCommand.Parameters); 
    }    
    process {
        $fetchExpression = New-Object -TypeName "Microsoft.Xrm.Sdk.Query.FetchExpression" -ArgumentList $FetchXml;
        $fetchExpression;
    }
    end {
        $StopWatch.Stop();
        Trace-XrmFunction -Name $MyInvocation.MyCommand.Name -Stage Stop -StopWatch $StopWatch;
    }
}

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