TenantHandling/Remove-NavContainerTenant.ps1

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
<#
 .Synopsis
  Removes a Tenant in a multitenant Nav container
 .Description
  Unmounts and removes a tenant database in the Nav container
 .Parameter containerName
  Name of the container in which you want to install the app (default navserver)
 .Parameter tenantId
  Name of tenant you want to remove in the container
 .Example
  Remove-NavContainerTenant -containerName test2 -tenantId mytenant
#>

function Remove-NavContainerTenant {
    Param
    (
        [Parameter(Mandatory=$false)]
        [string]$containerName = "navserver",
        [Parameter(Mandatory=$true)]
        [string]$tenantId,
        [System.Management.Automation.PSCredential]$sqlCredential = $null
    )

    Write-Host "Removing Tenant $tenantId from $containerName"

    $sqlCredential = Get-DefaultSqlCredential -containerName $containerName -sqlCredential $sqlCredential

    if ($tenantId -eq "tenant") {
        throw "You cannot remove a tenant called tenant"
    }

    $session = Get-NavContainerSession -containerName $containerName -silent
    Invoke-Command -Session $session -ScriptBlock { Param($tenantId, $sqlCredential)

        $customConfigFile = Join-Path (Get-Item "C:\Program Files\Microsoft Dynamics NAV\*\Service").FullName "CustomSettings.config"
        [xml]$customConfig = [System.IO.File]::ReadAllText($customConfigFile)
        if ($customConfig.SelectSingleNode("//appSettings/add[@key='Multitenant']").Value -ne "true") {
            throw "The NAV Container is not setup for multitenancy"
        }
        $databaseServer = $customConfig.SelectSingleNode("//appSettings/add[@key='DatabaseServer']").Value
        $databaseInstance = $customConfig.SelectSingleNode("//appSettings/add[@key='DatabaseInstance']").Value

        # Remove tenant
        Write-Host "Dismounting tenant $tenantId"
        Dismount-NavTenant -ServerInstance NAV -Tenant $TenantId -force | Out-null
        Remove-NavDatabase -DatabaseName $TenantId -DatabaseServer $databaseServer -DatabaseInstance $databaseInstance -DatabaseCredentials $sqlCredential

    } -ArgumentList $tenantId, $sqlCredential
    Write-Host -ForegroundColor Green "Tenant successfully removed"
}
Export-ModuleMember -Function Remove-NavContainerTenant