Misc/Wait-ForFinSqlToComplete.ps1

<#
 .Synopsis
  Waits for the process "finsql" to complete. Only for NAV2018
 .Description
  First, it will wait, if the process will be created within 10 seconds. If yes, it will wait until the process is completed
 .Parameter ContainerName
  Name of the container. Can be provided in the settings.json
 .Example
  Wait-ForFinSqlToComplete
#>

function Wait-ForFinSqlToComplete {
    Param(
        [Parameter(Mandatory=$false)]
        [string] $ContainerName
    )

    $ContainerName = Get-NewContainerName -ContainerName $ContainerName
    [version]$platform = Get-AppKeyValue -KeyName 'platform'

    if ($platform.Major -eq 11 -or $platform.Major -eq 14) {
        Invoke-ScriptInBcContainer -containerName $ContainerName -scriptblock {
            $success = $false
            $attempt = 1

            Write-Output "Checking for process finsql"
            while (!$success -and $attempt -le 10) {
                try {
                    $process = Get-Process -Name "finsql"
                    if ($null -eq $process) {
                        $success = $true
                    }
                }
                catch {
                    Write-Progress -CurrentOperation "WaitingForProcess" ( "Waiting ... " )
                    Start-Sleep -Seconds 1
                }
                $attempt = $attempt + 1
            }
            Write-Progress -CurrentOperation "WaitingForProcess" ( "Waiting ... Done" )

            if ($success) {
                Write-Output "Waiting for finsql to end (maximum of 10 minutes)"
                Wait-Process -id $process.Id -Timeout 600
            }

            Write-Output "Waiting completed"
        }
    }
}