Public/Search-NAVBinaries.ps1

function Search-NAVBinaries {
    <#
    .Synopsis
    Search NAV binaries, by looking at windows services.
 
    .Description
    Will look for all services with Name like MicrosoftDynamicsNavServer, and extract executable paths.
    #>

    [cmdletbinding()]
    param (
    )
    Process
    {
        $NavServerBinaryName = 'Microsoft.Dynamics.Nav.Server.exe'
        $PathNamePattern = '*'+$NavServerBinaryName+'*'
        $NavPaths = Get-WmiObject win32_service | Where-Object {$_.PathName -like $PathNamePattern -and $_.State -eq 'Running'} | Select-Object -ExpandProperty PathName

        $DistinctVersions = @{}
        foreach ($TestPath in $NavPaths) {
            $Length = $TestPath.indexof('\'+$NavServerBinaryName)
            $ServiceFolderPath = $TestPath.substring(1, $Length-1)

            $PathTokens = $ServiceFolderPath.split('\')

            $ServiceVersionCode = [int] $PathTokens[$PathTokens.count-2]
            if (-not $DistinctVersions.ContainsKey($ServiceVersionCode)) {
                $DistinctVersions.Add($ServiceVersionCode, $ServiceFolderPath)
            }
        }

        Write-Verbose 'We have found the following versions of NAV running as services on this machine:'
        #Write-Verbose $($DistinctVersions | Out-String)
 
        $DistinctVersions.GetEnumerator() | ForEach-Object {            
            $NAVVersion=$global:SupportedNAVVersionCodes[$_.Key]
            Write-Verbose (" {0,3} / {2,-6} : {1}" -F $_.Key, $_.Value, $NAVVersion)
        }

        return $DistinctVersions
    }
}