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.
    #>

    [cmdletbinding()]
    param (
    )
    Process
    {
        $DynamicsNavParentFolder = "$env:ProgramFiles\Microsoft Dynamics NAV" 
        $DynamicsNavParentFolder86 = "${env:ProgramFiles(x86)}\Microsoft Dynamics NAV"
        $DynamicsNavDataFolder = "$env:ProgramData\Microsoft\Microsoft Dynamics NAV"

        if (Test-Path $DynamicsNavParentFolder) {
            # read sub folders to find NavVersionCodes
            $NavVersionCodes = Get-ChildItem $DynamicsNavParentFolder
            
            if ($NavVersionCodes.Length -eq 1) {
                [int]$global:NavVersionCode = [int] $NavVersionCodes[0].Name 
                Write-Output "Version code found: $NavVersionCode"
            } elseif ($NavVersionCodes.Length -gt 1) {
                Write-Warning "Multiple versions of Dynamics NAV detected ($NavVersionCodes)."
                $maxVer = [int] $NavVersionCodes[0].Name
                foreach ($ver in $NavVersionCodes) {
                    $newVer = [int] $ver.Name
                    if ($newVer -gt $maxVer) {
                        $maxVer = $newVer
                    }
                }
                $global:NavVersionCode = $maxVer
                Write-Warning "The higher one ($maxVer) has been selected."
            } else {
                Write-Warning "Dynamics NAV is not installed on this host!"    
            }
        } else {
            Write-Warning "Dynamics NAV is not installed on this host!"
        }

        switch ($NavVersionCode) {
            71  { 
                $global:NavVersion="2013R2"
                $global:NavDefaultDatabase = 'Demo Database NAV (7-1)'         
            }
            80  { 
                $global:NavVersion="2015"
                $global:NavDefaultDatabase = 'Demo Database NAV (8-0)'
            }
            90  { 
                $global:NavVersion="2016"
                $global:NavDefaultDatabase = 'Demo Database NAV (9-0)'
            }
            100 { 
                $global:NavVersion="2017"
                $global:NavDefaultDatabase = 'Demo Database NAV (10-0)'
            }
            Default { 
                Write-Warning "This version is not supported: $NavVersionCode" 
                return
            }
        }

        $global:NavDefaultInstance = "DynamicsNAV$NavVersionCode"
        $global:NavDefaultCompanyName = 'CRONUS International Ltd.'
        $global:NavDefaultLicencePath = "$DynamicsNavDataFolder\$NavVersionCode\Database\Cronus.flf" 

        Write-Output "The version $NavVersion of Microsoft Dynamics NAV has been detected."

        if ($NavVersionCode -eq 71) {
            $global:DynamicsNavManagementModule = "$DynamicsNavParentFolder\$NavVersionCode\Service\NavAdminTool.ps1"
        } else {
            $global:DynamicsNavManagementModule = "$DynamicsNavParentFolder\$NavVersionCode\Service\Microsoft.Dynamics.Nav.Management.psm1"
            $global:DynamicsNavToolsModule = "$DynamicsNavParentFolder86\$NavVersionCode\RoleTailored Client\Microsoft.Dynamics.Nav.Model.Tools.psd1"

            #Import-Module $DynamicsNavToolsModule
            . "$DynamicsNavParentFolder86\$NAVVersionCode\RoleTailored Client\NavModelTools.ps1" -NavIde "$DynamicsNavParentFolder86\$NAVVersioncode\RoleTailored Client\finsql.exe" | Out-Null
            Write-Output "The Microsoft Dynamics NAV Tools module has been imported."
        }

        Import-Module $DynamicsNavManagementModule
        Write-Output "The Microsoft Dynamics NAV Management module has been imported."
    }
}