Modules/businessdev.ALbuild.OnPrem/Private/Invoke-BcManagementCommand.ps1
|
function Invoke-BcManagementCommand { <# .SYNOPSIS Invokes one Business Central management cmdlet, in-process where it loads natively and through a PowerShell 7 child process where it does not. .DESCRIPTION Single seam for every NAV management call the on-prem functions make. Import-BcManagementShell decides the mode once per session: InProcess - the cmdlets loaded into this session (BC <= 28 under Windows PowerShell 5.1 via the compatibility assembly, or any version when we already run under pwsh). Pwsh - only the .NET 8 Microsoft.BusinessCentral.* modules exist (BC29+) and we are on Windows PowerShell, which cannot load them: each call is delegated to pwsh (see Invoke-BcManagementProxyCommand). Routing on that recorded mode rather than on 'Get-Command <cmdlet>' keeps the decision in one place and avoids a per-call command lookup. .PARAMETER Command The management cmdlet, e.g. 'Publish-NAVApp'. .PARAMETER Parameters Parameters to splat into it. Include 'ErrorAction' explicitly when the call site depends on it - it is honoured in both modes. .OUTPUTS Whatever the cmdlet returned. In Pwsh mode objects are deserialised; property access is unaffected. #> [CmdletBinding()] param( [Parameter(Mandatory)] [ValidateNotNullOrEmpty()] [string] $Command, [hashtable] $Parameters = @{} ) Import-BcManagementShell -SentinelCommand $Command # StrictMode-safe: the state is set by Import-BcManagementShell, which we just called. $mode = if (Test-Path 'variable:script:BcManagementMode') { $script:BcManagementMode } else { 'InProcess' } if ($mode -ne 'Pwsh') { return & $Command @Parameters } Invoke-BcManagementProxyCommand -Command $Command -Parameters $Parameters ` -ModuleFile $script:BcManagementModuleFile -PwshPath $script:BcManagementPwsh } |