functions/common/Test-DynamicsNavModule.ps1

function Test-DynamicsNavModule {
    <#
    .SYNOPSIS
    Runs a module availability and command check for a Dynamics NAV module in the appropriate PowerShell version.
 
    .DESCRIPTION
    Determines whether to use Windows PowerShell (v5) or PowerShell Core (v7) based on the provided NAV platform version, and then invokes a script in the selected environment to test module availability and command readiness.
 
    .PARAMETER PlatformVersion
    The Dynamics NAV platform version (e.g., "210", "240").
 
    .PARAMETER ModuleType
    The type of module to test: "NavModelTools" or "NavAdminTool".
 
    .PARAMETER pwshPath
    Optional. Path to PowerShell 7 (pwsh.exe). Defaults to "C:\Program Files\PowerShell\7\pwsh.exe".
 
    .PARAMETER ps5Path
    Optional. Path to Windows PowerShell 5.1. Defaults to "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe".
 
    .OUTPUTS
    None. Outputs test results to the console.
 
    .EXAMPLE
    Test-DynamicsNavModule -PlatformVersion "240" -ModuleType "NavModelTools"
 
    Runs the module test using PowerShell 7 for platform version 240.
 
    .NOTES
    - This function is useful when working in mixed environments where different NAV versions require different PowerShell runtimes.
    - Depends on the BcAdmin module being available in the selected shell.
    #>


    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $true)]
        [string]$PlatformVersion,

        [Parameter(Mandatory = $true)]
        [ValidateSet("NavModelTools", "NavAdminTool")]
        [string]$ModuleType,

        [Parameter(Mandatory = $false)]
        $pwshPath = "C:\Program Files\PowerShell\7\pwsh.exe",

        [Parameter(Mandatory = $false)]
        $ps5Path = "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe"
    )    

    if ([int]$PlatformVersion -ge 240) {
        $powershellPath = $pwshPath
        $psVersion = '7'
    } else {
        $powershellPath = $ps5Path
        $psVersion = '5'
    }

    $script =  [ScriptBlock]::Create(@"
`$ErrorActionPreference = 'Stop'
Import-Module -Name BcAdmin
Test-DynamicsNavModuleAvailability -PlatformVersion $PlatformVersion -ModuleType $ModuleType
"@
)

    Write-Host "[$((Get-Date).ToString('HH:mm:ss'))] >> Starte Test von $ModuleType Version $PlatformVersion in PowerShell $psVersion..."
    & $powershellPath -NoLogo -NoProfile -Command $script
}