src/_Internals/Get-XrmFormXmlInternal.ps1

<#
    Read the FormXML of a systemform. With Unpublished, RetrieveUnpublishedMultiple returns the draft when the form has one,
    else the published form: a cmdlet that updates the FormXML reads it this way, so that it does not drop unpublished changes (M28).
    Returns { FormXml = [xml]; EntityLogicalName = table of the form }. Raises an error when the form does not exist.
#>

function Get-XrmFormXmlInternal {
    param(
        [Parameter(Mandatory = $true)]
        [Microsoft.PowerPlatform.Dataverse.Client.ServiceClient]
        $XrmClient,

        [Parameter(Mandatory = $true)]
        [Guid]
        $FormId,

        [Parameter(Mandatory = $false)]
        [switch]
        $Unpublished
    )

    $query = New-XrmQueryExpression -LogicalName "systemform" -Columns "formxml", "objecttypecode";
    $query = $query | Add-XrmQueryCondition -Field "formid" -Condition Equal -Values $FormId;
    $form = @($XrmClient | Get-XrmMultipleComponents -Query $query -Unpublished:$Unpublished) | Select-Object -First 1;
    if (-not $form) {
        throw "Form '$FormId' not found.";
    }

    [xml]$formXml = [string]$form.Record["formxml"];
    [PSCustomObject]@{
        FormXml           = $formXml;
        EntityLogicalName = [string]$form.Record["objecttypecode"];
    };
}