Public/Initialize-NAVVersion.ps1

function Initialize-NAVVersion {
    <#
    .Synopsis
    Detect and initialize NAV Version on the local machine.

    .Description
    Will look for a NAV installation and will set some common variables in the session context.

     .Parameter Version
    Dynamics NAV version(80,90,100 etc).
    #>

    [cmdletbinding()]
    param (
        [Parameter(Mandatory=$false)] [string]   $Version=""
    )
    Process
    {
        $DistinctVersions = Search-NAVBinaries

        if ($DistinctVersions.count -eq 1) {
            [int]$global:NavVersionCode = [int] ($DistinctVersions.GetEnumerator() | Select-Object -first 1).Key
            Write-Output "Version code found: $NavVersionCode"
        } elseif ($DistinctVersions.count -gt 1) {
            Write-Warning "Multiple versions of Dynamics NAV detected ($($DistinctVersions.Keys))."
            $maxVer = [int] ($DistinctVersions.GetEnumerator() | Select-Object -first 1).Key
           if($Version -eq "") {$flag = $true} else {$flag = $false}
            foreach ($ver in $DistinctVersions.GetEnumerator()) {
                $newVer = [int] $ver.Key
                if (($newVer -gt $maxVer) -AND $Version -eq "" ){
                    $maxVer = $newVer
                }elseif($Version -eq $newVer){
                    $maxVer = $newVer
                    $flag = $True 
                }
            }

            if(!$flag){
                Write-Error "Your setted the version $Version of Dynamics NAV and it's not installed on this host!"
                return
            }elseif($Version -ne ""){
                Write-Warning "The version ($maxVer) has been selected."                
            }else{
                Write-Warning "The higher one ($maxVer) has been selected."
            }

            $global:NavVersionCode = $maxVer
           
        } else {
            Write-Warning "Dynamics NAV is not installed on this host!"
            return
        }
        
        $global:DynamicsNavServiceParentFolder = $DistinctVersions[$global:NavVersionCode]
        $global:DynamicsNavRTCParentFolder = $global:DynamicsNavServiceParentFolder -replace "Program Files", "Program Files (x86)" -replace "Service", "RoleTailored Client"

        if ($global:SupportedNAVVersionCodes.ContainsKey($global:NavVersionCode)) {
            $global:NavVersion = $global:SupportedNAVVersionCodes[$global:NavVersionCode]
        } else {
            Write-Warning "This version is not supported: $NavVersionCode" 
            return
        }

        Write-Output "The version $NavVersion of Microsoft Dynamics NAV has been detected."
        Write-Output "Service folder path is: $global:DynamicsNavServiceParentFolder"
        Write-Output "RTC folder path is: $DynamicsNavRTCParentFolder"

        if ($NavVersionCode -eq 71) {
            $global:DynamicsNavManagementModule = "$global:DynamicsNavServiceParentFolder\NavAdminTool.ps1"
        } else {
            $global:DynamicsNavManagementModule = "$global:DynamicsNavserviceParentFolder\Microsoft.Dynamics.Nav.Management.psm1"
            $global:DynamicsNavToolsModule = "$global:DynamicsNavRTCParentFolder\Microsoft.Dynamics.Nav.Model.Tools.psd1"

            Import-Module $global:DynamicsNavToolsModule -WarningAction SilentlyContinue -Global | Out-Null

            #. "$global:DynamicsNavRTCParentFolder\NavModelTools.ps1" -NavIde "$global:DynamicsNavRTCParentFolder\finsql.exe" -Global | Out-Null
            Write-Output "The Microsoft Dynamics NAV Tools module has been imported."
        }

        Import-Module $DynamicsNavManagementModule -WarningAction SilentlyContinue -Global | Out-Null
        Write-Output "The Microsoft Dynamics NAV Management module has been imported."
    }
}