src/Solutions/Set-XrmSolutionVersion.ps1

<#
    .SYNOPSIS
    Set solution version.

    .DESCRIPTION
    Update specified solution by its uniquename with given version number.

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

    .PARAMETER SolutionUniqueName
    Solution unique name to update.

    .PARAMETER Version
    Version number to set.

    .OUTPUTS
    System.Void. A warning is displayed when the solution is not found.

    .EXAMPLE
    Set-XrmSolutionVersion -XrmClient $xrmClient -SolutionUniqueName "contoso_core" -Version "1.2.0.5";

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

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

        [Parameter(Mandatory = $true)]
        [ValidateNotNullOrEmpty()]
        [String]
        $SolutionUniqueName,

        [Parameter(Mandatory = $true)]
        [ValidateNotNullOrEmpty()]
        [String]
        $Version
    )
    begin {   
        $StopWatch = [System.Diagnostics.Stopwatch]::StartNew(); 
        Trace-XrmFunction -Name $MyInvocation.MyCommand.Name -Stage Start -Parameters ($MyInvocation.MyCommand.Parameters); 
    }    
    process {
        Write-HostAndLog -Message "Set version $Version to solution $($SolutionUniqueName)..." -Level INFO;
        $solution = $XrmClient | Get-XrmRecord -LogicalName "solution" -AttributeName "uniquename" -Value $SolutionUniqueName -Columns "version";
        if (-not $solution) {
            Write-HostAndLog -Message "Solution $SolutionUniqueName not found" -Level WARN;
            return $null;
        }
        $solutionToUpdate = New-XrmEntity -LogicalName "solution" -Id $solution.Id;
        $solutionToUpdate = $solutionToUpdate | Set-XrmAttributeValue -Name version -Value $Version;
        $solutionToUpdate | Update-XrmRecord -XrmClient $XrmClient;
        
        Write-HostAndLog -Message "Solution $SolutionUniqueName has been successfully updated to version : $Version!" -Level SUCCESS;
    }
    end {
        $StopWatch.Stop();
        Trace-XrmFunction -Name $MyInvocation.MyCommand.Name -Stage Stop -StopWatch $StopWatch;
    }    
}

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

Register-ArgumentCompleter -CommandName Set-XrmSolutionVersion -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;
}