Functions/Import-BcModule.ps1
function Import-BcModule { [CmdletBinding(DefaultParameterSetName='ServerInstance')] Param ( # Gets the modules compatible with the supplied serverinstance. [Parameter(ParameterSetName='ServerInstance', Mandatory = $true)] [string] $ServerInstance, # Gets the modules compatible with the supplied BcVersion. E.g. 'bc15' or 'bc13'. [Parameter(ParameterSetName='BcVersion', Mandatory = $true)] [string] $BcVersion, [switch] $ManagementModule, [switch] $AppManagementModule, [switch] $AppToolsModule, [switch] $Force ) # If not a specific module is specified import all modules. if( $ManagementModule -eq $false -and $AppManagementModule -eq $false -and $AppToolsModule -eq $false) { $ManagementModule = $true $AppManagementModule = $true $AppToolsModule = $true } if($BcVersion){ if((Get-BcVersion -BcVersion $BcVersion) -eq $false){ "The supplied BC/NAV version '{0}' is not valid. Valid values are for example 'BC15', 'BC13', 'NAV2017'" -f $BcVersion | Write-Warning return } [hashtable] $BcVersion = Get-BcVersion -BcVersion $BcVersion } # Get BC version from Service if($ServerInstance){ $ServerInstanceConfig = Get-BCServerInstance -ServerInstance $ServerInstance -WarningAction SilentlyContinue if(-not $ServerInstanceConfig -or $ServerInstanceConfig.Version -eq '1.0.0.0'){ 'Could not look up the version number for Server Instance ''{0}''. Make sure the Server Instance exists and the installation is valid.' -f $ServerInstance | Write-Warning return } $BcVersionFolder = ([version](Get-BCServerInstance -ServerInstance $ServerInstance).Version).Major * 10 [hashtable] $BcVersion = Get-BcVersion -VersionFolder $BcVersionFolder } # Disable app modules for Dynamics NAV. if($BcVersion.ProductAbb -eq 'NAV'){ $AppManagementModule = $false $AppToolsModule = $false } # Get installation folder $Service = Get-BcComponent -BcVersion $BcVersion.Version | Where-Object Component -eq 'NST' if (-not $Service.IsInstalled) { Write-Warning 'Cannot find the BC installation folder. Make sure the BC server component is installed.' return $false } $Modules = @() if($ManagementModule){ $Modules += 'Microsoft.Dynamics.Nav.Management' } if($AppManagementModule){ $Modules += 'Microsoft.Dynamics.Nav.Apps.Management' } if($AppManagementModule){ $Modules += 'Microsoft.Dynamics.Nav.Apps.Tools' } foreach($Module in $Modules){ $ModulePath = Join-Path -Path $Service.InstallLocation -ChildPath ($Module + ".psd1") if (-not (Test-Path -Path $ModulePath)) { $ModulePath = Join-Path -Path $NavService.InstallLocation -ChildPath ($Module + ".psm1") } if (-not (Test-Path -Path $ModulePath)) { 'Could not import module {0}. Module not found' -f $Module | Write-Warning continue } Import-Module $ModulePath -DisableNameChecking -Global -Force:$Force } } Export-ModuleMember -Function Import-BcModule |