functions/Deploy-App-OnPrem.psm1
function RIT-Deploy-App-OnPrem { Param( [string] $serviceTier = 'BC170', [string] $RoleTailoredPath = "C:\Program Files (x86)\Microsoft Dynamics 365 Business Central\170\RoleTailored Client\", [Parameter(Mandatory=$false)][string] $appFolderPath = '', [Parameter(Mandatory=$false)][switch] $ForceSync = $false, [Parameter(Mandatory=$false)][string] $Scope = 'Global', [Parameter(Mandatory=$false)][string] $Tenant = '' ) if ($appFolderPath -eq '') $appFolderPath = "$env:System_ArtifactsDirectory\*.app" $appPath = Get-ChildItem -Path $appFolderPath -Recurse $mgtPaths = @( @{ Primary = "$RoleTailoredPath\Microsoft.Dynamics.Nav.Apps.Management.psd1"; Fallback = "$RoleTailoredPath\Admin\Microsoft.Dynamics.Nav.Management.psm1"; }, @{ Primary = "$RoleTailoredPath\Microsoft.Dynamics.Nav.Management.dll"; Fallback = "$RoleTailoredPath\Admin\Microsoft.BusinessCentral.Apps.Management.dll"; } ) foreach ($mgtPath in $mgtPaths) { if (Test-Path $mgtPath.Primary) { Import-Module $mgtPath.Primary } else { Import-Module $mgtPath.Fallback } } $Instance = Get-NAVServerInstance $serviceTier -Force if($Instance.State -eq 'Stopped'){ Start-NAVServerInstance -ServerInstance $serviceTier -Force } $appPath | ForEach-Object { $navAppInfo = (Get-NAVAppInfo -Path $_) $oldappName = (Get-NAVAppInfo -ServerInstance $serviceTier -Name $navAppInfo.Name).Name if ($oldappName){ $oldVersion = (Get-NAVAppInfo -ServerInstance $serviceTier -Name $navAppInfo.Name).Version $oldVersion = $oldVersion[0].ToString() } $publishIt = $true $install = $true $upgrade = $true $appInfo = (Get-NAVAppInfo -ServerInstance $serviceTier -Name $navAppInfo.Name -Publisher $navAppInfo.Publisher -Version $navAppInfo.Version) if ($appInfo) { $publishIt = $false Write-Host "$($navAppInfo.Name) is already published" if ($appInfo.IsInstalled) { $install = $false $upgrade = $false Write-Host "$($navAppInfo.Name) is already installed" } } if ($publishIt) { Write-Host "Publishing $($navAppInfo.Name) (version $($navAppInfo.Version)) to $serviceTier" if ($Tenant) { Publish-NAVApp -ServerInstance $serviceTier -Path $_ -SkipVerification -Scope $Scope -Tenant $Tenant } else { Publish-NAVApp -ServerInstance $serviceTier -Path $_ -SkipVerification } } if ($install) { if ($ForceSync) { Write-Host "Syncing $serviceTier (force sync = true)" Sync-NAVTenant $serviceTier -Mode ForceSync -Force Sync-NAVApp -ServerInstance $serviceTier -Name $navAppInfo.Name -Version $navAppInfo.Version -Mode ForceSync -Force } else { Write-Host "Syncing $serviceTier (force sync = false)" Sync-NAVTenant $serviceTier -Mode Sync -Force Sync-NAVApp -ServerInstance $serviceTier -Name $navAppInfo.Name -Version $navAppInfo.Version -Force } if (!$oldappName) { Write-Host "Installing app $($navAppInfo.Name)" Install-NAVApp -ServerInstance $serviceTier -Name $navAppInfo.Name -Version $navAppInfo.Version } else { if ($oldVersion -ne $navAppInfo.Version[0].ToString()) { Write-Host "Running data upgrade for app $($navAppInfo.Name)" Start-NAVAppDataUpgrade -ServerInstance $serviceTier -Name $navAppInfo.Name -Version $navAppInfo.Version Write-Host "Unpublishing app $($navAppInfo.Name) (version $oldVersion)" try { Unpublish-NAVApp -ServerInstance $serviceTier -Name $navAppInfo.Name -Version $oldVersion } catch { Write-Host "Failed to unpublish $($navAppInfo.Name)" } } } } } } Export-ModuleMember -Function RIT-Deploy-App-OnPrem |