src/Solutions/Customization/Forms/Get-XrmFormControls.ps1

<#
    .SYNOPSIS
    List PCF custom controls configured on a form.

    .DESCRIPTION
    Parse the FormXML of a systemform record and return all custom control bindings (PCF controls): one row per control and form factor.
    The default control kept by each binding (customControl with an id) is not listed.

    .PARAMETER XrmClient
    Xrm connector initialized to target instance. Use latest one by default. (Dataverse ServiceClient)

    .PARAMETER FormReference
    EntityReference of the systemform record to inspect.

    .PARAMETER Unpublished
    Read the draft of the form when it has one (RetrieveUnpublishedMultiple). (Default: the published form)

    .OUTPUTS
    PSCustomObject[]. FieldName, ControlId (id of the control in the form), ControlName (PCF control name), FormFactor (0 = Web, 1 = Phone, 2 = Tablet) and Parameters (ordered hashtable: parameter name => value; the bound parameter holds the field name).

    .EXAMPLE
    $formRef = New-XrmEntityReference -LogicalName "systemform" -Id $formId;
    $controls = Get-XrmFormControls -FormReference $formRef;
    $controls | Format-Table;

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

function Get-XrmFormControls {
    [CmdletBinding()]
    [OutputType([PSCustomObject[]])]
    param
    (
        [Parameter(Mandatory = $false, ValueFromPipeline)]
        [Microsoft.PowerPlatform.Dataverse.Client.ServiceClient]
        $XrmClient = $Global:XrmClient,

        [Parameter(Mandatory = $true)]
        [ValidateNotNullOrEmpty()]
        [Microsoft.Xrm.Sdk.EntityReference]
        $FormReference,

        [Parameter(Mandatory = $false)]
        [switch]
        $Unpublished
    )
    begin {
        $StopWatch = [System.Diagnostics.Stopwatch]::StartNew();
        Trace-XrmFunction -Name $MyInvocation.MyCommand.Name -Stage Start -Parameters ($MyInvocation.MyCommand.Parameters);
    }
    process {
        $formXml = (Get-XrmFormXmlInternal -XrmClient $XrmClient -FormId $FormReference.Id -Unpublished:$Unpublished).FormXml;

        foreach ($description in $formXml.SelectNodes("//controlDescription")) {
            # forControl holds the uniqueid of the control
            $forControlId = $description.GetAttribute("forControl");
            $controlNode = @($formXml.SelectNodes("//control[@uniqueid]")) | Where-Object { $_.GetAttribute("uniqueid") -ieq $forControlId } | Select-Object -First 1;

            foreach ($customControl in $description.SelectNodes("customControl[@name]")) {
                $parameters = [ordered]@{};
                foreach ($parameterNode in $customControl.SelectNodes("parameters/*")) {
                    $parameters[$parameterNode.LocalName] = $parameterNode.InnerText;
                }
                [PSCustomObject]@{
                    FieldName   = if ($controlNode) { $controlNode.GetAttribute("datafieldname") } else { "" };
                    ControlId   = if ($controlNode) { $controlNode.GetAttribute("id") } else { $forControlId };
                    ControlName = $customControl.GetAttribute("name");
                    FormFactor  = [int]$customControl.GetAttribute("formFactor");
                    Parameters  = $parameters;
                };
            }
        }
    }
    end {
        $StopWatch.Stop();
        Trace-XrmFunction -Name $MyInvocation.MyCommand.Name -Stage Stop -StopWatch $StopWatch;
    }
}

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