Private/ReferenceData.ps1

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

    try {
        $ProgressPreference = 'SilentlyContinue'

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

        Write-Host $message

        Get-ConfigJSON($ScriptsFolder)

        Write-Host "Installing Data Management Tools..."
        Write-Host "Establishing connection to $global:devops_ServerUrl"

        Install-PAC

        Get-DataverseLogin
            
        # Create PAC Auth
        New-PACAuth

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

        if ($conn.IsReady) {
            try {
                #region PRE ACTION
                if (Test-Path -Path "$ScriptsFolder\ExportPreAction.ps1") {
                    Write-Host "Running Data Export Pre Action from $ScriptsFolder"
                    . $ScriptsFolder\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
                }

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

                    if (Test-Path -Path "$ScriptsFolder\ExportPostAction.ps1") {
                        Write-Host "Running Data Export Post Action from $ScriptsFolder"
                        . $ScriptsFolder\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
                    }
                    #endregion
                }
                else {
                    Write-PPDOMessage -Message "Data.zip file was not found. Review the output above for errors, or ensure there is data included in the configuration" -type "error"
                    pause
                }
            }
            catch {
                Write-Host $_
                pause
            }
            finally {
                Write-Host "Data Export Complete!"
            }
        }
    }
    catch {
        Write-Host $_
        pause
    }
}

function Get-ExportDataValid {
    Param(
        [string] [Parameter(Mandatory = $true)] $ReferenceDataFolder,
        [string] [Parameter(Mandatory = $true)] $ScriptsFolder
    )
    $ReferenceDataFolder = "$global:devops_projectLocation\$SolutionName\ReferenceData"
    
    if (!(Test-Path "$ReferenceDataFolder\schema.xml")) {
        if (!(Test-Path "$ReferenceDataFolder\data_schema.xml")) {
            Write-Host "schema.xml file not found, if you want to include Configuration Data, please run 'Configuration Data Create / Edit' first" -ForegroundColor Yellow
            return
        }
        Write-Host "Converting Legacy Data Export to Config Migration"
        Move-Item -Path "$ReferenceDataFolder\data_schema.xml" -Destination "$ReferenceDataFolder\schema.xml"
    }
    Invoke-ExportData -ReferenceDataFolder $ReferenceDataFolder -ScriptsFolder "$global:devops_projectLocation\$SolutionName\Scripts"
}

function Invoke-ImportReferenceData {
    Param(
        [Microsoft.Xrm.Tooling.Connector.CrmServiceClient] $CRMConn,
        [string] [Parameter(Mandatory = $true)] $PipelinePath,
        [string] [Parameter(Mandatory = $true)] $SolutionFolder,
        [string] [Parameter(Mandatory = $true)] $SolutionName,
        [bool] [Parameter(Mandatory = $true)] $VerboseLogging,
        [bool] [Parameter(Mandatory = $false)] $RunLocally = $false,
        [System.Collections.Hashtable] [Parameter(Mandatory = $true)] $Deploy
    )

    if ($Deploy.DeployData -eq $true) {                            

        Write-PPDOMessage "Importing reference Data for $SolutionName..." -Type group -RunLocally $RunLocally
        try {
            if (Test-Path -Path $PipelinePath\$SolutionFolder\ReferenceData\Extracted) {
                Write-PPDOMessage "Extracted Data Found... packaging for Import"
                Add-7zip $PipelinePath\$SolutionFolder\ReferenceData\Extracted\*.* $PipelinePath\$SolutionFolder\ReferenceData\data.zip
            }
            if (Test-Path -Path $PipelinePath\$SolutionFolder\ReferenceData\data.zip) {
                Write-PPDOMessage "Config data.zip found, importing now."
                if ($Deploy.LegacyDataTool) {
                    Write-Host "Importing Data using Legacy Data Tool"
                    if ($VerboseLogging) {
                        Import-CrmDataFile -CrmConnection $CRMConn -DataFile $PipelinePath\$SolutionFolder\ReferenceData\data.zip -ConcurrentThreads 5 -Verbose    
                    }
                    else {
                        Import-CrmDataFile -CrmConnection $CRMConn -DataFile $PipelinePath\$SolutionFolder\ReferenceData\data.zip -ConcurrentThreads 5 
                    }
                }
                else {
                    Write-Host "Importing Data using PAC Data"
                    & $env:APPDATA\Capgemini.PowerPlatform.DevOps\PACTools\tools\pac.exe data import --data $PipelinePath\$SolutionFolder\ReferenceData\data.zip 3>&1 | Tee-Object -Variable pacOutput
                }
            }
            else {
                Write-Host "Config Data file does not Exist"
            }
        }
        catch {
            Write-PPDOMessage "Unable to import configuration data - please review Pipeline error logs" -Type error -RunLocally $RunLocally -LogError $true
            Write-PPDOMessage "$($_.Exception.Message)" -Type error -RunLocally $RunLocally
            exit 1           
        }
    }                        
    else {
        Write-PPDOMessage "No Data to Import for $SolutionName" -Type section -RunLocally $RunLocally
    }
    Write-PPDOMessage -Type endgroup -RunLocally $RunLocally
}