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){ $BcVersionFolder = ([version](Get-BCServerInstance -ServerInstance $ServerInstance).Version).Major * 10 [hashtable] $BcVersion = Get-BcVersion -VersionFolder $BcVersionFolder } # Get installation folder $Service = Get-BcComponent -BcVersion $BcVersion.Version | Where-Object Component -eq 'NST' if (-not $Service.IsInstalled) { Write-Warning 'Cannot find BC module folder because the BC Service folder is not found. Make sure the BC Service 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 |