Patch/Cmdlets/NAV/Invoke-NAVRapidStartDataImport.ps1

<#
.SYNOPSIS
    Invoke RapidStart configuration package import
.DESCRIPTION
    To complete the data changes after the Microsoft Dynamics NAV data upgrade, run this function to import a RapidStart package containing necessary data for setting up the new version. Data from this package will be imported into required tables for all companies within the database.
.PARAMETER ServerInstance
    Specifies the Microsoft Dynamics NAV 2016 Server instance that is connected to the upgraded database
.PARAMETER RapidStartPackageFile
    Specifies the file path where the RapidStart Package is located
#>


function Invoke-NAVRapidStartDataImport
{
    [CmdletBinding()]
    param
    (
        [parameter(Mandatory=$true)]
        [string]$ServerInstance,

        [parameter(Mandatory=$true)]
        [string]$RapidStartPackageFile
    )
    PROCESS
    {
        $Companies = @()
        Get-NAVCompany -ServerInstance $ServerInstance | ForEach-Object { $Companies += $_.CompanyName }
        if($Companies.Count -eq 0)
        {
            Write-Error "No companies have been detected in tenant '$TenantID' mounted on instance '$ServerInstance"
            return
        }

        # Import the RapidStart package into all Companies in parallel
        Invoke-ImportRapidStartPackagePerCompanyOperations `
               -Companies $Companies `
               -ServerInstance $ServerInstance `
               -RapidStartPackageFile $RapidStartPackageFile

    }
}

Workflow Invoke-ImportRapidStartPackagePerCompanyOperations
{
    param
    (
        [parameter(Mandatory=$true)]
        [string[]]$Companies,

        [parameter(Mandatory=$true)]
        [string]$ServerInstance,

        [parameter(Mandatory=$true)]
        [string]$RapidStartPackageFile
    )
    foreach -parallel ($CompanyName in $Companies)
    {
        InlineScript `
        {
            Add-PSSnapin "Microsoft.Dynamics.Nav.Management"

            # Run RapidStart package import
            Invoke-NAVCodeunit `
                    -ServerInstance $Using:ServerInstance `
                    -CompanyName $Using:CompanyName `
                    -CodeunitId 8620 `
                    -MethodName "ImportAndApplyRapidStartPackage" `
                    -Argument $Using:RapidStartPackageFile

        } -MergeErrorToOutput $true
    }
}

Export-ModuleMember -Function Invoke-NAVRapidStartDataImport