Private/Select-Solution.ps1

function Invoke-ExportData {
    Param(
        [string] [Parameter(Mandatory = $true)] $StartPath,
        [string] [Parameter(Mandatory = $true)] $SelectedSolution
    )

    try {
        $ProgressPreference = 'SilentlyContinue'

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

        Write-Host $message
        Write-Host ""    

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

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

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

        Install-PAC

        Get-DataverseLogin
            
        #Create PAC Auth
        Create-PACAuth

        $conn = Get-DataverseConnection($global:devops_ServerUrl)

        if ($conn.IsReady) {
            try {
                # PRE ACTION
                if (Test-Path -Path $global:devops_projectLocation\$SolutionName\Scripts\ExportPreAction.ps1) {
                    Write-Host "Execute Pre Action from $global:devops_projectLocation\$SolutionName\Scripts"
                    . $global:devops_projectLocation\$SolutionName\Scripts\ExportPreAction.ps1 -Conn $conn
                }
                else {
                    Write-Host "Data Export PreAction not enabled, create a file called ExportPreAction.ps1 in the Scripts folder if you wish to use one" -ForegroundColor Yellow
                }

                # EXPORT
                Write-Host "Exporting Data to $global:devops_projectLocation\$SolutionName\ReferenceData\data.zip"
                $ProgressPreference = 'SilentlyContinue'
                & $env:APPDATA\Capgemini.PowerPlatform.DevOps\PACTools\tools\pac.exe data export --schemaFile "$global:devops_projectLocation\$SolutionName\ReferenceData\schema.xml" --dataFile "$global:devops_projectLocation\$SolutionName\ReferenceData\data.zip" --overwrite --verbose
                # Check that data export has successfully saved the file
                if (Test-Path -Path $global:devops_projectLocation\$SolutionName\ReferenceData\data.zip) {
                    Expand-Archive -Path $global:devops_projectLocation\$SolutionName\ReferenceData\data.zip -DestinationPath $global:devops_projectLocation\$SolutionName\ReferenceData\Extracted\ -Force
                    Remove-Item -Path $global:devops_projectLocation\$SolutionName\ReferenceData\data.zip -Force -ErrorAction SilentlyContinue

                    # POST ACTION
                    if (Test-Path -Path $global:devops_projectLocation\$SolutionName\Scripts\ExportPostAction.ps1) {
                        Write-Host "Execute Pre Action from $global:devops_projectLocation\$SolutionName\Scripts"
                        . $global:devops_projectLocation\$SolutionName\Scripts\ExportPostAction.ps1 -Conn $conn
                    }
                    else {
                        Write-Host "Data Export PostAction not enabled, create a file called ExportPostAction.ps1 in the Scripts folder if you wish to use one" -ForegroundColor Yellow
                    }
                } else {
                    Write-Host "Data.zip file was not found. Review the output above for errors, or ensure there is data included in the configuration"
                    pause
                }
            }
            catch {
                Write-Host $_
                pause
            }
            finally {
                Write-Host "Data Export Complete!!"
                pause
            }
            
        }

    }
    catch {
        Write-Host $_
        Pause
    }
    finally {
    }

}

function Get-ExportDataValid {
    if (!(Test-Path "$global:devops_projectLocation\$SolutionName\ReferenceData\schema.xml")) {
        if ((Test-Path "$global:devops_projectLocation\$SolutionName\ReferenceData\data_schema.xml")) {
            Write-Host "Converting Legacy Data Export to Config Migration"
            Move-Item -Path "$global:devops_projectLocation\$SolutionName\ReferenceData\data_schema.xml" -Destination "$global:devops_projectLocation\$SolutionName\ReferenceData\schema.xml"            
            Invoke-ExportData -StartPath $global:devops_projectLocation -SelectedSolution $selectedSolution
        }
        Write-Host ""
        Write-Host "Schema.xml file not found, if you want to include Configuration Data, please run 'Configuration Data Create / Edit' first" -ForegroundColor Yellow
    }
    else {
        Invoke-ExportData -StartPath $global:devops_projectLocation -SelectedSolution $selectedSolution
    }
}

function Select-Solution {
    if ($global:devops_projectFile.DataverseSolutions.Count -gt 0) {
        $options = $global:devops_projectFile.DataverseSolutions | ForEach-Object { $_.SolutionName }

        do {
            $sel = Invoke-Menu -MenuTitle "---- Please Select the Dataverse Solution to Manage ------" -MenuOptions $options
            $selectedSolution = $global:devops_projectFile.DataverseSolutions[$sel].SolutionName
        } until ($selectedSolution -ne "")

        Set-Location -Path $global:devops_projectLocation

        if ($global:devops_projectFile.ADOConfigured -eq "True") {
            try {
                git fetch --prune origin
                git pull origin
            }
            catch {
                pause
            }            
        }
        Show-SolutionMenu -SolutionName $selectedSolution
    }
}