Patch/Cmdlets/NAV/Invoke-NAVDataUpgrade.ps1

<#
.SYNOPSIS
    Invoke the Microsoft Dynamics NAV data upgrade process and wait until it completes.
.DESCRIPTION
    Invoke NAV Data Upgrade process and wait until it completes.
    Detected errors are displayed.
.PARAMETER ServerInstance
    Specifies the Microsoft Dynamics NAV Server instance that is going to execute the Microsoft Dynamics NAV data upgrade process.
.PARAMETER Tenant
    Specifies the tenant to upgrade.
#>


function Invoke-NAVDataUpgrade
{
    param
    (
        [parameter(Mandatory=$true)]
        [string]$ServerInstance,
        [parameter(Mandatory=$true,ValueFromPipeline)]
        [string]$Tenant
    )
    PROCESS
    {
            Start-NAVDataUpgrade -ServerInstance $ServerInstance -Tenant $Tenant `
                -ContinueOnError -SkipCompanyInitialization -FunctionExecutionMode Parallel `
                -SkipAppVersionCheck -SkipUserSessionCheck `
                -ErrorAction Stop -Force `

            # Wait for Upgrade Process to complete
            Get-NAVDataUpgrade -ServerInstance $ServerInstance -Tenant $Tenant -Progress

            # Make sure that Upgrade Process completed successfully.
            $errors = Get-NAVDataUpgrade -ServerInstance $ServerInstance -Tenant $Tenant -ErrorOnly

            if(!$errors)
            {
                # no errors detected - process has been completed successfully
                return;
            }

            # Stop the suspended process
            Stop-NAVDataUpgrade -ServerInstance $ServerInstance -Tenant $Tenant -Force

            $errorMessage = "Errors occurred during the Microsoft Dynamics NAV data upgrade process: " + [System.Environment]::NewLine
            foreach($nextErrorRecord in $errors)
            {
                $errorMessage += ("Codeunit ID: " + $nextErrorRecord.CodeunitId + ", Function: " + $nextErrorRecord.FunctionName + ", Error: " + $nextErrorRecord.Error + ", Company: " + $nextErrorRecord.CompanyName  +",Tenant: $Tenant" + [System.Environment]::NewLine)
            }

            Write-Error $errorMessage
    }
}

Export-ModuleMember -Function Invoke-NAVDataUpgrade