Functions/Solutions/Export-CdsSolution.ps1

<#
    .SYNOPSIS
    Export solution.
#>

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

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

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

        [Parameter(Mandatory = $false)]
        [ValidateScript( { Test-Path $_ })]
        [String]
        $ExportPath = $env:TEMP,

        [Parameter(Mandatory = $false)]
        [Boolean]
        $ExportCalendarSettings = $false,
        
        [Parameter(Mandatory = $false)]
        [Boolean]
        $ExportCustomizationSettings = $false,

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

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

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

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

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

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

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

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

        # Build solution file name
        $solutionFileName = $SolutionUniqueName;
        if ($AddVersionToFileName) {            
            $solution = $CdsClient | Get-CdsRecord -LogicalName "solution" -AttributeName "uniquename" -Value $SolutionUniqueName -Columns "version";
            $version = $solution.version;
            $version = $version.Replace(".", "_");
            $solutionFileName = "$($solutionFileName)_$($version)";
        }
        
        if ($managed) {
            $solutionFileName = "$($solutionFileName)_managed";
        }

        $solutionFileName = "$($solutionFileName).zip";

        # Build output path
        $solutionFilePath = [System.IO.Path]::Combine($ExportPath, $solutionFileName);

        # Build request
        $exportSolutionRequest = New-Object -TypeName Microsoft.Crm.Sdk.Messages.ExportSolutionRequest;
        $exportSolutionRequest.SolutionName = $SolutionUniqueName;
        $exportSolutionRequest.Managed = $Managed;    
        $exportSolutionRequest.ExportCalendarSettings = $ExportCalendarSettings;
        $exportSolutionRequest.ExportCustomizationSettings = $ExportCustomizationSettings;
        $exportSolutionRequest.ExportEmailTrackingSettings = $ExportEmailTrackingSettings;
        $exportSolutionRequest.ExportAutoNumberingSettings = $ExportAutoNumberingSettings;
        $exportSolutionRequest.ExportIsvConfig = $ExportIsvConfig;
        $exportSolutionRequest.ExportOutlookSynchronizationSettings = $ExportOutlookSynchronizationSettings;
        $exportSolutionRequest.ExportGeneralSettings = $ExportGeneralSettings;
        $exportSolutionRequest.ExportMarketingSettings = $ExportMarketingSettings;
        $exportSolutionRequest.ExportRelationshipRoles = $ExportRelationshipRoles;

        # Run request with extended timeout
        $CdsClient | Set-CdsClientTimeout -DurationInMinutes 15;
        $response = $CdsClient | Invoke-CdsRequest -Request $exportSolutionRequest;
        $CdsClient | Set-CdsClientTimeout -Revert;

        # Save solution file
        [System.IO.File]::WriteAllBytes($solutionFilePath, $response.ExportSolutionFile);

        # Output solution file path
        $solutionFilePath;
    }
    end {
        $StopWatch.Stop();
        Trace-CdsFunction -Name $MyInvocation.MyCommand.Name -Stage Stop -StopWatch $StopWatch;
    }    
}

Export-ModuleMember -Function Export-CdsSolution -Alias *;