functions/common/Get-DynamicsNavModule.ps1
function Get-DynamicsNavModule { <# .SYNOPSIS Retrieves the path or execution script block for NavModelTools.ps1 or NavAdminTool.ps1 based on the Dynamics NAV platform version. .DESCRIPTION This function locates and returns the appropriate Dynamics NAV PowerShell module script (`NavModelTools.ps1` or `NavAdminTool.ps1`) based on the provided platform version and module type. It supports returning either the file path or a script block that executes the tool silently (redirecting output to null). It also supports returning the path to finsql.exe when using NavModelTools. .PARAMETER PlatformVersion The Dynamics NAV platform version (e.g., "100" for NAV 2017). .PARAMETER ModuleType The type of NAV module to retrieve. Must be either "NavModelTools" or "NavAdminTool". .PARAMETER ReturnPath If specified, the function returns the file path to the tool script instead of a script block. .PARAMETER ReturnFinsqlPath If specified (only valid with ModuleType "NavModelTools"), returns the path to finsql.exe instead of the script or script block. .OUTPUTS ScriptBlock or String. Returns either a script block that can be executed, or a file path depending on the parameters. .EXAMPLE Get-DynamicsNavModule -PlatformVersion "100" -ModuleType "NavModelTools" Returns a script block to execute NavModelTools.ps1 silently for platform version 100. .EXAMPLE Get-DynamicsNavModule -PlatformVersion "100" -ModuleType "NavAdminTool" -ReturnPath Returns the full file path to NavAdminTool.ps1 for platform version 100. .EXAMPLE Get-DynamicsNavModule -PlatformVersion "100" -ModuleType "NavModelTools" -ReturnFinsqlPath Returns the full path to finsql.exe for the specified platform version. .NOTES - The function reads installation paths from the Windows Registry. - It handles both the RoleTailored Client (NavModelTools) and the NAV/BC Service (NavAdminTool). - If the tool script or finsql.exe cannot be found, the function throws a detailed error message. #> [CmdletBinding()] param ( [Parameter(Mandatory = $true)] [string]$PlatformVersion, [Parameter(Mandatory = $true)] [ValidateSet("NavModelTools", "NavAdminTool")] [string]$ModuleType, [switch]$ReturnPath, [switch]$ReturnFinsqlPath ) if ($ModuleType -eq "NavModelTools") { $regPath = "HKLM:\SOFTWARE\Wow6432Node\Microsoft\Microsoft Dynamics NAV\$PlatformVersion\RoleTailored Client" try { $props = Get-ItemProperty -Path $regPath -ErrorAction Stop $installPath = $props.Path } catch { throw "NAV Client-Installation konnte nicht gefunden werden (Registry: $regPath)." } $toolScript = Join-Path $installPath 'NavModelTools.ps1' $finsqlPath = Join-Path $installPath 'finsql.exe' if (-not (Test-Path $toolScript)) { throw "Script '$toolScript' wurde nicht gefunden." } if (-not (Test-Path $finsqlPath)) { throw "finsql.exe wurde nicht gefunden: $finsqlPath" } if (-not $ReturnPath -and -not $ReturnFinsqlPath) { return [ScriptBlock]::Create(@" & '$toolScript' -NavIde '$finsqlPath' > `$null 2>&1 "@) } else { if (-not $ReturnFinsqlPath) { return [string]$toolScript } else { return [string]$finsqlPath } } } elseif ($ModuleType -eq "NavAdminTool") { $regPath = "HKLM:\SOFTWARE\Microsoft\Microsoft Dynamics NAV\$PlatformVersion\Service" try { $props = Get-ItemProperty -Path $regPath -ErrorAction Stop $installPath = $props.Path } catch { throw "NAV/BC Service-Installation konnte nicht gefunden werden (Registry: $regPath)." } if (-not (Test-Path $installPath)) { throw "Installationsverzeichnis existiert nicht: $installPath" } $adminPath = Join-Path -Path $installPath -ChildPath Admin $managementPath = Join-Path -Path $installPath -ChildPath Management if (Test-Path -Path $adminPath) { $searchToolPath = $adminPath } elseif (Test-Path -Path $managementPath) { $searchToolPath = $managementPath } else { $searchToolPath = $installPath } # Suche rekursiv nach NavAdminTool.ps1 $toolScript = Get-ChildItem -Path $searchToolPath -Recurse -Filter 'NavAdminTool.ps1' -ErrorAction SilentlyContinue | Select-Object -First 1 if (-not $toolScript) { throw "NavAdminTool.ps1 wurde im Pfad '$searchToolPath' nicht gefunden." } if (-not $ReturnPath) { return [ScriptBlock]::Create(@" & '$($toolScript.FullName)' > `$null 2>&1 "@) } else { return [string]$toolScript.FullName } } } |