functions/Deploy-App-OnPrem.psm1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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)][switch] $ForceSync = $false
)

$appPath = Get-ChildItem -Path "$env:System_ArtifactsDirectory\*.app" -Recurse

Import-Module $RoleTailoredPath\"Microsoft.Dynamics.Nav.Apps.Management.psd1"
Import-Module $RoleTailoredPath\"Microsoft.Dynamics.Nav.Management.dll"


$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"
        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