Scripts/SolutionExport.ps1

# SolutionExport.ps1

function Invoke-ExportSolution {
  Param(
    [string] [Parameter(Mandatory = $true)] $StartPath,
    [string] [Parameter(Mandatory = $true)] $SelectedSolution,
    [string] [Parameter(Mandatory = $false)] $UserName = "",
    [string] [Parameter(Mandatory = $false)] $Password = "",
    [System.Obsolete("TenantId is now fetched from the project file directly.")]
    [string] [Parameter(Mandatory = $false)] $TenantId = "",
    [bool] [Parameter(Mandatory = $false)] $UseClientSecret = $false
  )

  try {
    if (!$global:devops_FullTool) {
      $global:devops_projectLocation = $StartPath

      Set-ToolVariables
      if (!$global:devops_projectFile) {
        $repoName = ($global:devops_configFile.Projects | Where-Object { $_.ProjectLocation -eq $global:devops_projectLocation })[0].Name
        Write-Verbose "Fetched repo name '$repoName' by looking up project in config file by ProjectLocation"
        $global:devops_projectFile = Get-Content ("$global:devops_projectLocation\$repoName.json") | ConvertFrom-Json
      }
      else {
        Write-Verbose "Project File global variable already set"
      }
      Set-ProjectVariables

      Install-ConfigMigrationModule
      Install-PowerAppsAdmin
      Install-XrmModule

      if ($UserName -ne "") {
        $global:devops_DataverseEmail = $UserName
      }

      if ($Password -ne "") {
        $global:Password = $Password
      }

      if ($UseClientSecret -eq $true) {
        $global:devops_DataverseCredType = "servicePrincipal"
        $global:devops_ClientID = $UserName
        $global:clientSecret = $Password
      }
      else {
        $global:devops_DataverseCredType = "user"
      }
    }

    $ProgressPreference = 'SilentlyContinue'

    $message = @"
 ____ _ _ _ _____ _
/ ___| ___ | |_ _| |_(_) ___ _ __ | ____|_ ___ __ ___ _ __| |_
\___ \ / _ \| | | | | __| |/ _ \| '_ \ | _| \ \/ / '_ \ / _ \| '__| __|
 ___) | (_) | | |_| | |_| | (_) | | | | | |___ > <| |_) | (_) | | | |_
|____/ \___/|_|\__,_|\__|_|\___/|_| |_| |_____/_/\_\ .__/ \___/|_| \__|
                                                   |_|
"@

    Write-Host $message
    Write-Host ""

    Get-ConfigJSON("$StartPath\$SelectedSolution\Scripts")

    Write-Verbose "Installing PAC"
    Install-PAC

    Write-Verbose "Getting Dataverse Connection"
    $conn = Get-DataverseConnection($global:devops_ServerUrl)

    Write-Verbose "Creating PAC Auth"
    New-PACAuth

    Write-Verbose "Testing if conn is ready"
    if ($conn.IsReady) {
      Write-Verbose "Attempting to fetch solutions from CRM"
      
      Get-DataverseSolution "$StartPath\$SelectedSolution" $SelectedSolution
    }
  }
  catch {
    Write-Host $_
    pause
  }
  finally {
    #region Cleaning Up
    Write-Host "Cleaning Up..."
    
    Remove-Item (Join-Path $StartPath "nuget.exe") -ErrorAction Ignore -Force
    Remove-Item (Join-Path $StartPath "Tools") -Force -Recurse -ErrorAction Ignore
    Remove-Item (Join-Path $StartPath "*.zip") -Force -ErrorAction Ignore -Exclude "Publisher.zip"
    & $env:APPDATA\Capgemini.PowerPlatform.DevOps\PACTools\tools\pac.exe auth delete --name ppdo
    Write-Host "Complete ... Enjoy !!!"
    If (!$UserName) {
      pause
    }
    #endregion
  }
}