src/AzDevOps/BuildTasks/Backup-XrmSolutionsBuild.ps1

<#
    .SYNOPSIS
    Run build action to unpack solutions

    .DESCRIPTION
    This cmdlet is designed to be fully intergrated in Az DevOps pipeline.
    This cmdlet export given solutions and then start SolutionPackager extract action to working directory.

    .PARAMETER ConnectionString
    Target instance connection string, use variable 'ConnectionString' from associated variable group.

    .PARAMETER UnpackPath
    Folder path where solutions will be extracted. (Default: Agent working directory)

    .PARAMETER Solutions
    Solution uniquenames that will be exported and then unpacked, use variable 'Solutions' from associated variable group.

    .PARAMETER Managed
    Specify if solution should be export as managed or unmanaged. (Default: false = unmanaged)

    .PARAMETER DefaultExportPath
    Folder path where solutions will be exported before unpacked. (Default: Agent temp directory)

    .PARAMETER TimeoutInMinutes
    Maximum wait for the export system job of each solution, in minutes. (Default: 10)

    .OUTPUTS
    System.String. Folder where each solution was unpacked, one per solution; progress and the SolutionPackager lines are written to the host and to the log.

    .EXAMPLE
    Backup-XrmSolutionsBuild -ConnectionString $connectionString -Solutions "contoso_core,contoso_sales" -UnpackPath "C:\src\contoso\solutions";

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

function Backup-XrmSolutionsBuild {
    [CmdletBinding()]
    [OutputType([System.String])]
    param
    (
        [Parameter(Mandatory = $false)]
        [String]
        $ConnectionString = $env:CONNECTIONSTRING,
        
        [Parameter(Mandatory = $false)]
        [String]
        $UnpackPath = $env:SYSTEM_DEFAULTWORKINGDIRECTORY,

        [Parameter(Mandatory = $false)]
        [String]
        $Solutions = $env:SOLUTIONS,

        [Parameter(Mandatory = $false)]
        [Boolean]
        $Managed = $false,

        [Parameter(Mandatory = $false)]
        [String]
        $DefaultExportPath = [System.IO.Path]::GetTempPath(),

        [Parameter(Mandatory = $false)]
        [int]
        $TimeoutInMinutes = 10
    )
    begin {   
        $StopWatch = [System.Diagnostics.Stopwatch]::StartNew(); 
        Trace-XrmFunction -Name $MyInvocation.MyCommand.Name -Stage Start -Parameters ($MyInvocation.MyCommand.Parameters); 
    }    
    process {
        # Relative to the PowerShell location, not to the folder of the process (M25)
        if ($DefaultExportPath) { $DefaultExportPath = Resolve-XrmPathInternal -Path $DefaultExportPath; }
        if ($UnpackPath) { $UnpackPath = Resolve-XrmPathInternal -Path $UnpackPath; }
        $XrmClient = New-XrmClient -ConnectionString $ConnectionString;
        $solutionList = $Solutions.Split(",");
        $solutionList | ForEach-Object {
            
            Write-HostAndLog -Message "Exporting solution $($_)" -Level INFO;           
            $solutionFilePath = $XrmClient | Export-XrmSolution -SolutionUniqueName $_ -Managed $Managed -ExportPath $DefaultExportPath -ExportCalendarSettings $true -ExportCustomizationSettings $true -ExportEmailTrackingSettings $true  -ExportAutoNumberingSettings $true  -ExportIsvConfig $true  -ExportOutlookSynchronizationSettings $true  -ExportGeneralSettings $true  -ExportMarketingSettings $true  -ExportRelationshipRoles $true -TimeoutInMinutes $TimeoutInMinutes;                
            Write-HostAndLog -Message "Solution $($_) successfully exported to '$solutionFilePath' !" -Level SUCCESS;

            Write-HostAndLog -Message "Unpacking solution $($_)" -Level INFO;   
            $solutionUnpackPath = [System.IO.Path]::Combine("$UnpackPath", $_);
            # SolutionPackager lines go to the pipeline log, not to the output of the task (B38)
            Invoke-XrmSolutionPackager -Action Extract -SolutionFilePath $solutionFilePath -FolderPath $solutionUnpackPath | Out-Host;
            Write-HostAndLog -Message "Solution $($_) successfully unpacked to '$solutionUnpackPath' !" -Level SUCCESS;
            $solutionUnpackPath;
        }
    }
    end {
        $StopWatch.Stop();
        Trace-XrmFunction -Name $MyInvocation.MyCommand.Name -Stage Stop -StopWatch $StopWatch;
    }    
}

Export-ModuleMember -Function Backup-XrmSolutionsBuild -Alias *;