Scripts/ExportSolution.ps1

#
# ExportSolution.ps1
#

function Get-DataverseSolution {
    Param(
        [string] [Parameter(Mandatory = $true)] $StartPath,
        [string] [Parameter(Mandatory = $true)] $SelectedSolution
    )
    try {
        
        $SolutionName = $SelectedSolution
        Remove-Item (Join-Path $StartPath "\dataverse_*patch*") -Force -Recurse -ErrorAction Ignore        
        ######################## EXPORT SOLUTION

        ##Export Patches if they Exist
        foreach ($PatchSolution in $PatchQuery.CrmRecords) {
            $SolutionId = $PatchSolution.solutionid
            $SolutionName = $PatchSolution.uniquename
            $SolutionVersion = $PatchSolution.version

            $message = "Exporting Unmanaged Solution for $SolutionName"
            Write-Host $message

            Export-CrmSolution -SolutionName $SolutionName -SolutionZipFileName "$SolutionName.zip" -conn $conn -ErrorAction Stop

            $message = "Exporting Managed Solution for $SolutionName"
            Write-Host $message

            Export-CrmSolution -SolutionName $SolutionName -Managed -SolutionZipFileName $SolutionName"_managed.zip" -conn $conn -ErrorAction Stop

            ######################## EXTRACT SOLUTION
            # $ErrorActionPreference = "SilentlyContinue"
            
            $message = "Unpacking Solution $SolutionName"
            Write-Host $message
            Remove-Item (Join-Path $StartPath "\dataverse_$SolutionName\") -Recurse -Force -ErrorAction Ignore
            & $env:APPDATA\Microsoft.PowerPlatform.DevOps\PACTools\tools\pac.exe solution unpack --folder (Join-Path $StartPath "\dataverse_$SolutionName\") --zipfile "$SolutionName.zip" --packagetype Both --allowDelete $false --clobber --useUnmanagedFileForMissingManaged
            

            $canvasApps = Get-ChildItem -Path  (Join-Path $StartPath "\dataverse_$SolutionName\") -Filter *.msapp -Recurse
            try {
                # unpack canvas apps
                $canvasApps | ForEach-Object {
                    Write-Host "Unpacking Canvas App $($_.name)";
                    & $env:APPDATA\Microsoft.PowerPlatform.DevOps\PACTools\tools\pac.exe canvas unpack --msapp $_.FullName --sources "$($_.DirectoryName)\$($_.BaseName)"
                    Remove-Item $_.FullName -Force -ErrorAction SilentlyContinue
                }
            }
            catch {
                Write-Host "Error in Unpacking Canvas App"
                Write-Host $_
            }
 
        }
    
        ## No Patches
        If (!$PatchQuery.CrmRecords) {
            $message = "Exporting Unmanaged Solution for $SolutionName"
            Write-Host $message

            Export-CrmSolution -SolutionName $SolutionName -SolutionZipFileName "$SolutionName.zip" -conn $conn -ErrorAction Stop

            $message = "Exporting Managed Solution for $SolutionName"
            Write-Host $message

            Export-CrmSolution -SolutionName $SolutionName -Managed -SolutionZipFileName $SolutionName"_managed.zip" -conn $conn -ErrorAction Stop
        
            ######################## EXTRACT SOLUTION
            # $ErrorActionPreference = "SilentlyContinue"


            $message = "Unpacking Solution $SolutionName"
            Write-Host $message
            Remove-Item (Join-Path $StartPath "\dataverse_$SolutionName\") -Recurse -Force -ErrorAction Ignore
            & $env:APPDATA\Microsoft.PowerPlatform.DevOps\PACTools\tools\pac.exe solution unpack --folder (Join-Path $StartPath "\dataverse_$SolutionName\") --zipfile "$SolutionName.zip" --packagetype Both --allowDelete $false --clobber --useUnmanagedFileForMissingManaged
            $canvasApps = Get-ChildItem -Path  (Join-Path $StartPath "\dataverse_$SolutionName\") -Filter *.msapp -Recurse
    
            # unpack canvas apps
            $canvasApps | ForEach-Object {
                Write-Host "Unpacking Canvas App $($_.name)";
                & $env:APPDATA\Microsoft.PowerPlatform.DevOps\PACTools\tools\pac.exe canvas unpack --msapp $_.FullName --sources "$($_.DirectoryName)\$($_.BaseName)"
                Remove-Item $_.FullName -Force -ErrorAction SilentlyContinue
            }
        
        }
    
    }
    catch {
        Write-Host $_
        pause
    }
    finally {

    }
}

function Get-FlowsToBeDeployed {
    Param(
        [string] [Parameter(Mandatory = $true)] $StartPath,
        [string] [Parameter(Mandatory = $true)] $SelectedSolution
    )
    try {
        Write-Host "Generating Flows_Default.json to Support Flow Activation"
        $SolutionName = $SelectedSolution
        $SolutionPath = (Join-Path $StartPath "\dataverse_$SolutionName\Workflows")

        $Workflows = Get-ChildItem -Path $SolutionPath -Filter *.json -ErrorAction SilentlyContinue
        if ($Workflows) {
            $Workflows | ForEach-Object {
                $FlowName = $_.BaseName.SubString(0, $_.BaseName.Length - 36)
                $FlowID = $_.BaseName.Replace($FlowName, '')
                $FlowJSON += @([ordered]@{FlowID = $FlowID; FlowName = $FlowName; ActivateAsUser = ""; })
            }
            ConvertTo-Json -Depth 3 $FlowJSON | Format-Json | Out-File $StartPath\Flows_Default.json            
        }

    }
    catch {
        Write-Host $_
        pause
    }
}