src/Solutions/Clear-XrmSolutions.ps1

<#
    .SYNOPSIS
    Select solutions to uninstall.

    .DESCRIPTION
    Select solutions (managed or unmanaged) and uninstall them one after the other (see Uninstall-XrmSolution: system job, watched until its end).
    A solution that fails is displayed and the next one is processed.

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

    .PARAMETER Columns
    Specify expected columns to retrieve. (Default : id, uniquename, friendlyname, version, ismanaged, installedon, createdby, publisherid, modifiedon, modifiedby)

    .PARAMETER TimeOutInMinutes
    Maximum wait for each solution uninstall, in minutes. The job keeps running on the platform after it. (Default: 45)

    .OUTPUTS
    System.Void.

    .EXAMPLE
    Clear-XrmSolutions -XrmClient $xrmClient -TimeOutInMinutes 90;

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

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

        [Parameter(Mandatory = $false)]
        [ValidateNotNullOrEmpty()]
        [String[]]
        $Columns = @("solutionid", "uniquename", "friendlyname", "version", "ismanaged", "installedon", "createdby", "publisherid", "modifiedon", "modifiedby"),
        
        [Parameter(Mandatory = $false)]
        [ValidateRange(1, 1440)]
        [int]
        $TimeOutInMinutes = 45
    )
    begin {   
        $StopWatch = [System.Diagnostics.Stopwatch]::StartNew(); 
        Trace-XrmFunction -Name $MyInvocation.MyCommand.Name -Stage Start -Parameters ($MyInvocation.MyCommand.Parameters); 
    }    
    process {
        
        # uniquename identifies the solution to uninstall
        $selectedColumns = @($Columns) + "uniquename" | Select-Object -Unique;
        $solutionsToRemove = Select-XrmSolutions -XrmClient $XrmClient -Columns $selectedColumns -OutputMode Multiple;

        ForEach-ObjectWithProgress -Collection $solutionsToRemove -OperationName "Uninstall solutions" -ScriptBlock {
            param($solution)

            Write-HostAndLog " > Removing solution " -NoNewline -NoTimeStamp -ForegroundColor Gray;
            Write-HostAndLog $solution.friendlyname -NoNewline -NoTimeStamp -ForegroundColor Yellow;
            Write-HostAndLog " ..." -NoNewline -NoTimeStamp -ForegroundColor Gray;

            $stopWatch = [System.Diagnostics.Stopwatch]::StartNew();
            try {
                $XrmClient | Uninstall-XrmSolution -SolutionUniqueName $solution.uniquename -TimeoutInMinutes $TimeOutInMinutes;
                $stopWatch.Stop();
                
                Write-Host "[OK] (Duration = $($stopWatch.Elapsed.ToString("g")))" -ForegroundColor Green;
            }
            catch {
                $stopWatch.Stop();
                Write-Host "[KO : $($_.Exception.Message)] (Duration = $($stopWatch.Elapsed.ToString("g")))" -ForegroundColor Red;
            }
        }
    }
    end {
        $StopWatch.Stop();
        Trace-XrmFunction -Name $MyInvocation.MyCommand.Name -Stage Stop -StopWatch $StopWatch;
    }    
}

Export-ModuleMember -Function Clear-XrmSolutions -Alias *;