Functions/Solutions/Start-CdsSolutionUpgrade.ps1

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

function Start-CdsSolutionUpgrade {
    [CmdletBinding()]
    param
    (
        [Parameter(Mandatory=$false, ValueFromPipeline)]
        [Microsoft.Xrm.Tooling.Connector.CrmServiceClient]
        $CdsClient = $Global:CdsClient,

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

        $deleteAndPromoteRequest = New-CdsRequest -Name "DeleteAndPromote ";
        $deleteAndPromoteRequest | Add-CdsRequestParameter -Name "UniqueName" -Value $SolutionUniqueName | Out-Null;
        
        try 
        {            
            $deleteAndPromoteResponse = $CdsClient | Invoke-CdsRequest -Request $deleteAndPromoteRequest -Async;
            $asyncOperationId =  $deleteAndPromoteResponse.AsyncJobId;
            Watch-CdsAsynchOperation -AsyncOperationId $asyncOperationId;
        }
        catch {
            $errorMessage = $_.Exception.Message;
            Write-HostAndLog "$($MyInvocation.MyCommand.Name) => KO : [Error: $errorMessage]" -ForegroundColor Red -Level FAIL;
            write-progress one one -completed;
            throw $errorMessage;
        }  
    }
    end {
        $StopWatch.Stop();
        Trace-CdsFunction -Name $MyInvocation.MyCommand.Name -Stage Stop -StopWatch $StopWatch;
    }    
}

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