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 -DemoDataPath ~/MyProject/Apps -companyName TestCompany
.NOTES
    The command uses commands from BcContainerHelper to import backup of the objects
#>

function Restore-CompanyData {
    [CmdletBinding()]
    Param (

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

    #Chck precondition
    Test-Preconditions

    #check BC container version
    $version = (Get-BcContainerNavVersion -containerOrImageName $containerName).Split(".")[0]
    $rootModulePath = Split-Path -Path $PSScriptRoot -Parent
    switch ($version) {

        "14" { $appFile = Get-ChildItem (Join-Path -Path $rootModulePath -ChildPath (Join-Path -Path "SourceApp" -ChildPath "BC14")) -Depth 2 -Filter "*.app" | Select-Object -ExpandProperty FullName }
        "15" { $appFile = Get-ChildItem (Join-Path -Path $rootModulePath -ChildPath (Join-Path -Path "SourceApp" -ChildPath "BC15")) -Depth 2 -Filter "*.app" | Select-Object -ExpandProperty FullName }
        "16" { $appFile = Get-ChildItem (Join-Path -Path $rootModulePath -ChildPath (Join-Path -Path "SourceApp" -ChildPath "BC15")) -Depth 2 -Filter "*.app" | Select-Object -ExpandProperty FullName }
        "17" { $appFile = Get-ChildItem (Join-Path -Path $rootModulePath -ChildPath (Join-Path -Path "SourceApp" -ChildPath "BC15")) -Depth 2 -Filter "*.app" | Select-Object -ExpandProperty FullName }
        default { Throw "Selected BC version: $version is not supported" }
    }

    #initialize variables
    $appName = "DemoDataManager"

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

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

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

    #uninstall DemoDataManager app if exists
    $AppInfo = Get-BcContainerAppInfo $ContainerName | Where-Object { $_.Name -eq $appName }
    if ($AppInfo.Name -like $appName) {
        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
}
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 $DemoDataPath)) {

        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) -eq $companyName) {

        Throw "Selected company does not exist."
    }
}

Export-ModuleMember -Function Restore-CompanyData