Import-ALOpsNavManagementDlls.ps1

function Import-ALOpsNavManagementDlls()
{
    param(
        [string] $ServerInstance
    )

    Write-Host "*** Import NAV/BC Management DLL's"
        
    $ServiceFolder = ""
    if ([string]::IsNullOrEmpty($ServerInstance)){
        Write-Host "*** ServiceTier not specified, loading default assemblies."
        $ServiceFolder = "C:\Program Files\Microsoft Dynamics*\*\Service"
    } else {
        Write-Host "*** Loading assemblies for ServiceTier [$($ServerInstance)]."
        $ServiceFolder = Get-BCServicePath -ServerInstance $ServerInstance
        Write-Host "*** Loading assemblies from: [$ServiceFolder]"        
    }    

    Write-Host "*** Load module [Microsoft.Dynamics.Nav.Management]"
    $FoundDLLPath = $null
    $FoundDLLPaths = Get-childItem $ServiceFolder -Filter "Microsoft.Dynamics.Nav.Management.dll" -Recurse | Where-Object { -not $_.FullName.Contains("LegacyDlls") } 
    $FoundDLLPath = $FoundDLLPaths | Where-Object { $_.FullName  -ilike "*Management*" }  | Select-Object -First 1
    if ($null -eq $FoundDLLPath){
        $FoundDLLPath = $FoundDLLPaths | Select-Object -First 1
    }
    Import-Module "$($FoundDLLPath.FullName)"   

    Write-Host "*** Load module [Microsoft.Dynamics.Nav.Apps.Management]"
    $FoundDLLPath = $null
    $FoundDLLPaths = Get-childItem $ServiceFolder -Filter "Microsoft.Dynamics.Nav.Apps.Management.dll" -Recurse | Where-Object { -not $_.FullName.Contains("LegacyDlls") } 
    $FoundDLLPath = $FoundDLLPaths | Where-Object { $_.FullName  -ilike "*Management*" }  | Select-Object -First 1
    if ($null -eq $FoundDLLPath){
        $FoundDLLPath = $FoundDLLPaths | Select-Object -First 1
    }
    Import-Module "$($FoundDLLPath.FullName)" 
}