src/Solutions/Customization/Commands/Set-XrmCommand.ps1

<#
    .SYNOPSIS
    Update a command bar button in Microsoft Dataverse.

    .DESCRIPTION
    Update an existing appaction record (command bar button).

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

    .PARAMETER CommandReference
    EntityReference of the appaction to update.

    .PARAMETER Name
    Ignored, with a warning: the platform does not update the name of a command after its creation. Change the text shown to users with -ButtonLabelText.

    .PARAMETER Labels
    Ignored, with a warning, as -Name.

    .PARAMETER LanguageCode
    Ignored, as -Labels.

    .PARAMETER ButtonLabelText
    Updated button label text.

    .PARAMETER TooltipTitle
    Updated tooltip title text (buttontooltiptitle).

    .PARAMETER Hidden
    Updated hidden state.

    .PARAMETER SolutionUniqueName
    Unmanaged solution unique name. When provided, the updated command is automatically added to this solution (component type read from the organization, see Get-XrmSolutionComponentType).

    .OUTPUTS
    Microsoft.Xrm.Sdk.EntityReference. Reference to the updated appaction record.

    .EXAMPLE
    Set-XrmCommand -CommandReference $cmdRef -ButtonLabelText "Approve Request";
    Set-XrmCommand -CommandReference $cmdRef -ButtonLabelText "Approve Request" -SolutionUniqueName "MySolution";

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

function Set-XrmCommand {
    [CmdletBinding(SupportsShouldProcess)]
    [OutputType([Microsoft.Xrm.Sdk.EntityReference])]
    param
    (
        [Parameter(Mandatory = $false, ValueFromPipeline)]
        [Microsoft.PowerPlatform.Dataverse.Client.ServiceClient]
        $XrmClient = $Global:XrmClient,

        [Parameter(Mandatory = $true)]
        [ValidateNotNull()]
        [Microsoft.Xrm.Sdk.EntityReference]
        $CommandReference,

        [Parameter(Mandatory = $false)]
        [string]
        $Name,

        [Parameter(Mandatory = $false)]
        [Hashtable]
        $Labels,

        [Parameter(Mandatory = $false)]
        [int]
        $LanguageCode = 1033,

        [Parameter(Mandatory = $false)]
        [string]
        $ButtonLabelText,

        [Parameter(Mandatory = $false)]
        [string]
        $TooltipTitle,

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

        [Parameter(Mandatory = $false)]
        [string]
        $SolutionUniqueName
    )
    begin {
        $StopWatch = [System.Diagnostics.Stopwatch]::StartNew();
        Trace-XrmFunction -Name $MyInvocation.MyCommand.Name -Stage Start -Parameters ($MyInvocation.MyCommand.Parameters);
    }
    process {
        $record = New-XrmEntity -LogicalName "appaction" -Id $CommandReference.Id;

        if ($PSBoundParameters.ContainsKey('Name') -or $PSBoundParameters.ContainsKey('Labels')) {
            # name is not valid for update: the platform ignores it without error
            Write-Warning "The name of a command cannot be changed after its creation: -Name / -Labels ignored. Use -ButtonLabelText for the text shown to users.";
        }
        if ($PSBoundParameters.ContainsKey('ButtonLabelText')) {
            $record.Attributes["buttonlabeltext"] = $ButtonLabelText;
        }
        if ($PSBoundParameters.ContainsKey('TooltipTitle')) {
            $record.Attributes["buttontooltiptitle"] = $TooltipTitle;
        }
        if ($PSBoundParameters.ContainsKey('Hidden')) {
            $record.Attributes["hidden"] = $Hidden;
        }

        if ($record.Attributes.Count -gt 0) {
            Update-XrmRecord -XrmClient $XrmClient -Record $record;
        }

        if ($PSBoundParameters.ContainsKey('SolutionUniqueName')) {
            $componentType = Get-XrmSolutionComponentType -XrmClient $XrmClient -LogicalName "appaction";
            Add-XrmSolutionComponent -XrmClient $XrmClient -SolutionUniqueName $SolutionUniqueName -ComponentId $CommandReference.Id -ComponentType $componentType -DoNotIncludeSubcomponents $false | Out-Null;
        }

        $CommandReference;
    }
    end {
        $StopWatch.Stop();
        Trace-XrmFunction -Name $MyInvocation.MyCommand.Name -Stage Stop -StopWatch $StopWatch;
    }
}

Export-ModuleMember -Function Set-XrmCommand -Alias *;