ObjectFilesHandling/Restore-CompanyData.ps1

<#
.SYNOPSIS
    Import company data from selected tables from json files
.DESCRIPTION
    Import backup of all objects to BC
.EXAMPLE
    Restore-CompanyData -containerName bccontainer -Path ~/MyProject/Apps -companyName TestCompany
.NOTES
    The command uses commands from BcContainerHelper to import backup of the objects
#>

function Install-DemoDataManagerApp {
    Param (

        [string] $containerName,
        [string] $appName,
        [string] $appFile
    )

    Publish-BcContainerApp -containerName $containerName -appFile $appFile -skipVerification -scope Tenant
    Sync-BcContainerApp -containerName $containerName -appName $appName -Mode ForceSync -Force
    Install-BcContainerApp -containerName $containerName -appName $appName -Force
}
function Uninstall-DemoDataManagerApp {
    Param(

        [string] $appName
    )

    UnInstall-BcContainerApp -containerName $containerName -appName $appName
    Sync-BcContainerApp -containerName $containerName -appName $appName-Mode Clean -Force
    UnPublish-BcContainerApp -containerName $containerName -appName $appName -doNotSaveData -force
}

function Test-Preconditions {

    if (!(Test-Path $path)) {

        Throw "Selected Demo Data path is missing! If you left parameter empty, make sure that folder 'DemoData' is present in your working directory."
    }

    if (!((docker ps) -match $containerName)) {

        Throw "Selected container is either not existing or not started."
    }

    if ((Get-CompanyInBcContainer $containerName | Select-Object -ExpandProperty CompanyName) -contains $companyName) {

        Throw "Selected company does not exist."
    }
}
function Restore-CompanyData {
    [CmdletBinding()]
    Param (

        [string]$path = './DemoData',
        [Parameter(Mandatory = $true)]
        [string]$containerName,
        [Parameter(Mandatory = $true)]
        [string]$companyName
    )

    #Chck precondition
    Test-Preconditions

    #initialize variables
    $appFile = Get-ChildItem (Join-Path ($PSScriptRoot, "SourceApp")) -Depth 2 -Filter "*.app" | Select-Object -ExpandProperty FullName
    $appName = "DemoDataManager"

    #remove old backup archive if exists
    Remove-Item (Join-Path $path, "CompanyBackup.zip")

    #compress all json files to new archive
    Write-Host 'Compress json data files' -ForegroundColor Green
    Compress-Archive -Path (Join-Path $path, "*") -DestinationPath 'CompanyBackup.zip'

    #send created compressed file to the container
    Write-Host 'Send archive to container' -ForegroundColor Green
    Copy-FileToBcContainer -localPath (Join-Path $path, "CompanyBackup.zip") -containerPath 'c:\CompanyBackup.zip' -containerName $ContainerName

    #uninstall DemoDataManager app if exists
    $AppInfo = Get-BcContainerAppInfo $ContainerName | Where-Object { $_.Name -eq $appName }
    if ($AppInfo.AppId -like "7672c184-b8c4-4437-8e22-e6ec5fc5fd53") {
        Uninstall-DemoDataManagerApp -appName $appName
    }
    #publish and install DemoDataManager app
    Write-Host 'Publish and install Demo Data Manager App' -ForegroundColor Green
    Install-DemoDataManagerApp -containerName $containerName -appName $appName -appFile $appFile

    #restore data from archive and delete DemoDataManager app after restoration process
    Write-Host 'Restore company data' -ForegroundColor Green
    Invoke-NavContainerCodeunit -containerName $ContainerName -Codeunitid 90006 -MethodName StartRestoreCompanyData -Argument 'c:\CompanyBackup.zip' -CompanyName $companyName
    Uninstall-DemoDataManagerApp -appName $appName
}

Export-ModuleMember -Function Restore-CompanyData