functions/Export-RegentSolution.ps1

Import-Module "$PSScriptRoot\Get-Config.ps1"
Import-Module crm-datatools

function Export-RegentSolution {
  <#
    .SYNOPSIS
    Exports a solution from the specified CRM and unpacks it into source control.
     
    .DESCRIPTION
    This command is used to export a solution from a specified CRM into the local solutions repository and unpack the solution into its constituent parts. Both managed and unamanged versions are exported so as to allow either to recreated at a later time. Additionally, if a new version is specified, the version of the solution is updated in the specified CRM BEFORE exporting.
 
    .EXAMPLE
    Export the solution "MyImportantSolution" from CRMRECRUITTEST
    Export-RegentSolution -SolutionName MyImportantSolution -CrmInstance CRMRECRUITTEST
 
    .EXAMPLE
    Export the solution "MyOtherImportantSolution" from CRMADVISETEST and update the version to 1.5.5
    Export-RegentSolution -SolutionName MyOtherImportantSolution -CrmInstance CRMADVISETEST -UpdateVersion "1.5.5"
 
  #>

  param(
    # The unique CRM solution name
    [Parameter(Mandatory = $true)]
    [string]$SolutionName,
    
    # The connection object (usually generated by Get-RegentConnection)
    [Parameter(Mandatory = $true)]
    $conn,

    # The new version of the solution. Setting this will update it in the development crm instance
    [string]$UpdateVersion 
  )

  $ErrorActionPreference = "Stop"
  # Find path to solution packager
  $solutionPackager = (Split-Path $PsScriptRoot -Parent) + "\lib\SolutionPackager.exe"
  # Determine CRM Connection String

  Write-Verbose "Getting Config:"
  $Config = Get-Config
  $RepositoryPath = $Config.RepositoryPath # The path to Git repository with the unpacked solutions
  Write-Verbose "RepositoryPath: $RepositoryPath"

  #Update Version if applicable
  if ($UpdateVersion) {
    Write-Host "Updating Solution Version to $UpdateVersion"
    Set-CrmSolutionVersionNumber `
      -SolutionName $SolutionName `
      -Version $UpdateVersion `
      -conn $conn
    Write-Host "Solution Version Updated"
  }

  Write-Host "Downloading Solution: $SolutionName"

  #Export Solutions
  $unmanagedSolution = Export-CrmSolution `
    -conn $conn `
    -SolutionName $SolutionName `
    -SolutionZipFileName "$SolutionName.zip" `
    -SolutionFilePath $RepositoryPath  
  Write-Host "Exported unmanaged solution to $($unmanagedSolution.SolutionPath)"
  $managedSolution = Export-CrmSolution `
    -conn $conn `
    -SolutionName $SolutionName `
    -SolutionZipFileName "$($SolutionName)_managed.zip"`
    -SolutionFilePath $RepositoryPath `
    -Managed
  Write-Host "Exported managed solution to $($managedSolution.SolutionPath)"

  #Unpack the solution
  Write-Host "Unpacking Solution"
  $extractOuput = & "$solutionPackager" /action:Extract /zipfile:"$($unmanagedSolution.SolutionPath)" /folder:"$(Split-Path -Parent $unmanagedSolution.SolutionPath)/$SolutionName" /packagetype:Both /errorlevel:Info /allowWrite:Yes /allowDelete:Yes
  # Delete the unnecessary .zip files
  Remove-Item -Path "$RepositoryPath/*" -Filter "$SolutionName*.zip"

  Write-Host $extractOuput
  if ($lastexitcode -ne 0) {
    throw "Solution Extract operation failed with exit code: $lastexitcode"
  }
  else {
    # At one point I had it check for warnings... I can't remember why.
    # if (($extractOuput -ne $null) -and ($extractOuput -like "*warnings encountered*")) {
    # throw "Solution Packager encountered warnings. Check the output."
    # }
    # else {
    Write-Host "Solution Pack Completed Successfully."

    # }
  }

  return $($unmanagedSolution, $managedSolution)
}