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 = "",
    [string] [Parameter(Mandatory = $false)] $TenantId = "",
    [bool] [Parameter(Mandatory = $false)] $UseClientSecret = $false
  )

  try {
    if (!$global:devops_FullTool) {
      $global:devops_projectLocation = $StartPath
      Install-ConfigMigrationModule
      Install-PowerAppsAdmin
      Install-XrmModule

      If ($UserName -ne "") {
        $global:devops_DataverseEmail = $UserName
      }
      If ($Password -ne "") {
        $global:Password = $Password
      }
      If ($TenantId -ne "") {
        $global:devops_TenantID = $TenantId
      }
      If ($UseClientSecret -eq $true) {
        $global:devops_DataverseCredType = "servicePrincipal"
        $global:devops_ClientID = $UserName
        $global:clientSecret = $Password
  
      }
      If ($UseClientSecret -eq $false) {
        $global:devops_DataverseCredType = "user"
      }
  
    }

    $ProgressPreference = 'SilentlyContinue'

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

    Write-Host $message
    Write-Host ""    

    Write-Host "Running for path : " $StartPath
    Get-ConfigJSON($StartPath)

    $message = "Installing Solution Management Tools..."
    Write-Host $message

    Install-PAC

    $message = "Establishing connection to $global:devops_ServerUrl"
    Write-Host $message

    $conn = Get-DataverseConnection($global:devops_ServerUrl)
        
    #Create PAC Auth
    if ($global:devops_DataverseCredType -eq "servicePrincipal") {
      & $env:APPDATA\Capgemini.PowerPlatform.DevOps\PACTools\tools\pac.exe auth delete --name ppdo
      # Added escaped quotes required to get secrets working that started with - (https://github.com/dylanhaskins/Capgemini.PowerPlatform.DevOps/issues/150)
      & $env:APPDATA\Capgemini.PowerPlatform.DevOps\PACTools\tools\pac.exe auth create --applicationId $global:devops_ClientID --clientSecret `"$global:clientSecret`" --environment $global:devops_ServerUrl  --tenant $global:devops_TenantID  --name ppdo
      #--name $global:devops_SolutionName
    }
    if ($global:devops_DataverseCredType -eq "user") {
      & $env:APPDATA\Capgemini.PowerPlatform.DevOps\PACTools\tools\pac.exe auth delete --name ppdo
      & $env:APPDATA\Capgemini.PowerPlatform.DevOps\PACTools\tools\pac.exe auth create --username $global:devops_DataverseEmail --password $global:Password --environment $global:devops_ServerUrl --name ppdo
      #--name $global:devops_SolutionName
    }

    if ($conn.IsReady) {

      ######################## CHECK SOLUTION
      # Get solution by name
      $SolutionQuery = Get-CrmRecords -conn $conn -EntityLogicalName solution -Fields 'friendlyname', 'version', 'uniquename' -FilterAttribute uniquename -FilterOperator eq -FilterValue $global:devops_SolutionName
      $Solution = $SolutionQuery.CrmRecords[0]
      if (!$Solution) { throw "Solution not found: $global:devops_SolutionName" }
      $SolutionId = $Solution.solutionid
      $SolutionVersion = $Solution.version
      $SolutionName = $Solution.uniquename
      Write-Host "Found:" $SolutionId "-" $Solution.friendlyname  "-" $SolutionVersion

      # Get most recent patch solution
      Write-Host "Checking for Patch Solutions ..."
      $PatchQuery = Get-CrmRecordsByFetch -conn $conn @"
<fetch>
  <entity name="solution" >
    <attribute name="uniquename" />
    <attribute name="friendlyname" />
    <attribute name="version" />
    <filter>
      <condition attribute="parentsolutionid" operator="eq" value="$SolutionId" />
    </filter>
    <order attribute="createdon" descending="false" />
  </entity>
</fetch>
"@
 


      Get-DataverseSolution "$StartPath\$SelectedSolution" $SelectedSolution

    }
  }
  catch {
    Write-Host $_
    pause
  }
  finally {
    ######################### CLEANING UP #########################
    $message = "Cleaning Up..."
    Write-Host $message
    
    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        
    $message = "Complete ... Enjoy !!!"
    Write-Host $message
    If (!$UserName) {
      pause
    }
        
  }
}