functions/common/Import-DynamicsNavModule.ps1

function Import-DynamicsNavModule {
<#
.SYNOPSIS
Importiert Microsoft Dynamics NAV/Business Central PowerShell-Module anhand der Plattformversion und des Modultyps.
 
.DESCRIPTION
Diese Funktion identifiziert den Installationspfad basierend auf der angegebenen Plattformversion
und lädt gezielt ein gewünschtes Modul wie Management, Apps.Management, Apps.Tools oder Model.Tools.
 
.PARAMETER PlatformVersion
Die exakte Plattformversion (z. B. '140', '200', '240'), wie sie in der Registry hinterlegt ist.
 
.PARAMETER ModuleType
Der Modultyp, der importiert werden soll.
Mögliche Werte:
- Management
- Apps.Management
- Apps.Tools
- Model.Tools
 
Der Standardwert ist 'Management'.
 
.EXAMPLE
Import-DynamicsNavModule -PlatformVersion '200'
 
.EXAMPLE
Import-DynamicsNavModule -PlatformVersion '240' -ModuleType 'Apps.Tools'
 
.OUTPUTS
Kein direkter Rückgabewert. Importiert das Modul in die aktuelle Sitzung.
 
.NOTES
Diese Funktion ersetzt die veraltete Funktion 'Import-BcManagement'.
#>


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

        [Parameter(Mandatory = $false)]
        [ValidateSet("Management", "Apps.Management", "Apps.Tools", "Model.Tools")]
        [string]$ModuleType = "Management"
    )

    if ($ModuleType -ne "Model.Tools")
    {
        $nstsPath = 'HKLM:\SOFTWARE\Microsoft\Microsoft Dynamics NAV\'
        $InstalledPlatforms = Get-ChildItem -Path $nstsPath | ForEach-Object { $_.PSChildName }

        if (-not ($PlatformVersion -in $InstalledPlatforms)) {
            throw "Microsoft Dynamics NAV/BC platform $PlatformVersion is not installed on the local machine!"
        }

        $serviceRegPath = "HKLM:\SOFTWARE\Microsoft\Microsoft Dynamics NAV\$PlatformVersion\Service"
        $serviceProps = Get-ItemProperty -Path $serviceRegPath

        if ($serviceProps.Installed -ne 1) {
            throw "Microsoft Dynamics NAV/BC platform $PlatformVersion is not installed or the Service is missing!"
        }

        $servicePath = $serviceProps.Path

        if (-not (Test-Path $servicePath)) {
            throw "Service path '$servicePath' does not exist!"
        }
    } elseif ($ModuleType -eq "Model.Tools") {
        $rtcsPath = 'HKLM:\SOFTWARE\Wow6432Node\Microsoft\Microsoft Dynamics NAV\'
        $InstalledPlatforms = Get-ChildItem -Path $rtcsPath | ForEach-Object { $_.PSChildName }

        if (-not ($PlatformVersion -in $InstalledPlatforms)) {
            throw "Microsoft Dynamics NAV/BC RTC $PlatformVersion is not installed on the local machine!"
        }

        $rtcRegPath = "HKLM:\SOFTWARE\Wow6432Node\Microsoft\Microsoft Dynamics NAV\$PlatformVersion\RoleTailored Client"
        $rtcProps = Get-ItemProperty -Path $rtcRegPath

        if ($rtcProps.Installed -ne 1) {
            throw "Microsoft Dynamics NAV/BC RTC $PlatformVersion is not installed or the Service is missing!"
        }

        $rtcPath = $rtcProps.Path

        if (-not (Test-Path $rtcPath)) {
            throw "Service path '$rtcPath' does not exist!"
        }
    }

    # Basenamen je nach Modultyp definieren
    $baseNames = @()
    switch ($ModuleType) {
        "Management" { 
            $searchPath = $servicePath
            $baseNames = @(
                "Microsoft.Dynamics.Nav.Management.psd1",
                "Microsoft.BusinessCentral.Management.psd1"
            )
        }
        "Model.Tools" {
            $searchPath = $rtcPath
            $baseNames = @(
                "Microsoft.Dynamics.Nav.Model.Tools.psd1"
            )
        }
        "Apps.Management" {
            $searchPath = $servicePath
            $baseNames = @(
                "Microsoft.Dynamics.Nav.Apps.Management.psd1",
                "Microsoft.BusinessCentral.Apps.Management.psd1"
            )
        }
        "Apps.Tools" {
            $searchPath = $servicePath
            $baseNames = @(
                "Microsoft.Dynamics.Nav.Apps.Tools.psd1",
                "Microsoft.BusinessCentral.Apps.Tools.psd1"
            )
        }
    }

    $foundFile = Get-ChildItem -Path $searchPath -Recurse -ErrorAction SilentlyContinue |
        Where-Object { -not $_.PSIsContainer -and ($_.Name -in $baseNames) } |
        Select-Object -First 1

    if (-not $foundFile) {
        throw "No valid NAV/BC PowerShell module file (*.psd1) for type '$ModuleType' found in '$searchPath'!"
    }

    Import-Module $foundFile.FullName -Force
}