src/Solutions/Start-XrmSolutionUpgrade.ps1

<#
    .SYNOPSIS
    Start delete and promote operation for solution.

    .DESCRIPTION
    Replace managed solution by new one after import.

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

    .PARAMETER SolutionUniqueName
    Solution unique name to upgrade.

    .OUTPUTS
    System.Void. The cmdlet waits for the DeleteAndPromote system job and throws when it fails.

    .EXAMPLE
    Import-XrmSolution -XrmClient $xrmClient -SolutionUniqueName "contoso_core" -SolutionFilePath "C:\Temp\contoso_core_managed.zip" -Upgrade $true;
    Start-XrmSolutionUpgrade -XrmClient $xrmClient -SolutionUniqueName "contoso_core";

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

function Start-XrmSolutionUpgrade {
    [CmdletBinding(SupportsShouldProcess)]
    [OutputType([System.Void])]
    param
    (
        [Parameter(Mandatory = $false, ValueFromPipeline)]
        [Microsoft.PowerPlatform.Dataverse.Client.ServiceClient]
        $XrmClient = $Global:XrmClient,

        [Parameter(Mandatory = $true)]
        [ValidateNotNullOrEmpty()]
        [String]
        $SolutionUniqueName
    )
    begin {   
        $StopWatch = [System.Diagnostics.Stopwatch]::StartNew(); 
        Trace-XrmFunction -Name $MyInvocation.MyCommand.Name -Stage Start -Parameters ($MyInvocation.MyCommand.Parameters); 
    }    
    process {

        $deleteAndPromoteRequest = New-XrmRequest -Name "DeleteAndPromote";
        $deleteAndPromoteRequest | Add-XrmRequestParameter -Name "UniqueName" -Value $SolutionUniqueName | Out-Null;
        
        try {            
            $deleteAndPromoteResponse = $XrmClient | Invoke-XrmRequest -Request $deleteAndPromoteRequest -Async;
            if ($WhatIfPreference -and $null -eq $deleteAndPromoteResponse) { return; }
            $asyncOperationId = $deleteAndPromoteResponse.AsyncJobId;
            $XrmClient | Watch-XrmAsynchOperation -AsyncOperationId $asyncOperationId -ThrowOnFailure | Out-Null;
        }
        catch {
            $errorMessage = $_.Exception.Message;
            Write-HostAndLog "$($MyInvocation.MyCommand.Name) => KO : [Error: $errorMessage]" -ForegroundColor Red -Level FAIL;
            throw $errorMessage;
        }  
    }
    end {
        $StopWatch.Stop();
        Trace-XrmFunction -Name $MyInvocation.MyCommand.Name -Stage Stop -StopWatch $StopWatch;
    }    
}

Export-ModuleMember -Function Start-XrmSolutionUpgrade -Alias *;

Register-ArgumentCompleter -CommandName Start-XrmSolutionUpgrade -ParameterName "SolutionUniqueName" -ScriptBlock {

    param($CommandName, $ParameterName, $WordToComplete, $CommandAst, $FakeBoundParameters)

    $solutionUniqueNames = @();
    $solutions = Get-XrmSolutions -Columns "uniquename";
    $solutions | ForEach-Object { $solutionUniqueNames += $_.uniquename };
    return $solutionUniqueNames | Where-Object { $_ -like "$wordToComplete*" } | Sort-Object;
}