forked from microsoft/navcontainerhelper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSet-BcContainerFeatureKeys.ps1
109 lines (104 loc) · 5.12 KB
/
Set-BcContainerFeatureKeys.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
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<#
.Synopsis
Set Feature Keys in container
.Description
Enumerates hash table and sets the feature keys in a container tenant database
.Parameter containerName
Name of the container in which you want to set feature keys
.Parameter tenant
Tenant in which you want to set feature keys
.Parameter featureKeys
Hashtable of featureKeys you want to set
.Example
Set-BcContainerFeatureKeys -containerName test2 -featureKeys @{"EmailHandlingImprovements" = "None"}
#>
function Set-BcContainerFeatureKeys {
Param (
[Parameter(Mandatory=$false)]
[string] $containerName = $bcContainerHelperConfig.defaultContainerName,
[Parameter(Mandatory=$false)]
[string] $tenant = "*",
[Parameter(Mandatory=$true)]
[hashtable] $featureKeys
)
$telemetryScope = InitTelemetryScope -name $MyInvocation.InvocationName -parameterValues $PSBoundParameters -includeParameters @("featureKeys")
try {
if ($featureKeys.Keys.Count -ne 0) {
Invoke-ScriptInBCContainer -containerName $containerName -ScriptBlock { Param([string] $tenant, [hashtable] $featureKeys)
$customConfigFile = Join-Path (Get-Item "C:\Program Files\Microsoft Dynamics NAV\*\Service").FullName "CustomSettings.config"
[xml]$customConfig = [System.IO.File]::ReadAllText($customConfigFile)
$databaseServer = $customConfig.SelectSingleNode("//appSettings/add[@key='DatabaseServer']").Value
$databaseInstance = $customConfig.SelectSingleNode("//appSettings/add[@key='DatabaseInstance']").Value
$databaseName = $customConfig.SelectSingleNode("//appSettings/add[@key='DatabaseName']").Value
$multitenant = $customConfig.SelectSingleNode("//appSettings/add[@key='Multitenant']").Value -eq "true"
if ($databaseServer -ne "localhost" -or $databaseInstance -ne "SQLEXPRESS") {
Write-Host "WARNING: Trying to use Set-BcContainerFeatureKeys on a foreign database, no feature keys are set"
exit
}
if (!$multitenant) {
$databases = @($databaseName)
}
else {
if ($tenant -eq "*") {
$databases = @(Get-NAVTenant -ServerInstance $serverinstance | % { $_.Id })
$databases += @("tenant")
}
else {
$databases = @($tenant)
}
}
$databases | % {
$databaseName = $_
Write-Host "Setting feature keys on database: $databaseName"
$featureKeys.Keys | % {
$featureKey = $_
$enabledStr = $featureKeys[$featureKey]
if ($enabledStr -eq "All Users" -or $enabledStr -eq "1") {
$enabled = 1
}
elseif ($enabledStr -eq "None" -or $enabledStr -eq "0") {
$enabled = 0
}
else {
$enabled = -1
Write-Host "WARNING: Unknown value ($enabledStr) for feature key $featureKey"
}
if ($enabled -ne -1) {
try {
#Create new record in table of feature setup table [Tenant Feature Key] in case it is missing
$SQLRecord = Invoke-Sqlcmd -Database $databaseName -Query "SELECT * FROM [dbo].[Tenant Feature Key] where ID = '$featureKey'"
if ([String]::IsNullOrEmpty($SQLRecord))
{
Write-host "Creating record for feature ID '$featureKey'"
$SQLcolumns = "ID, Enabled"
$SQLvalues = "'$featureKey',0"
Invoke-Sqlcmd -Database $databaseName -Query "INSERT INTO [CRONUS].[dbo].[Tenant Feature Key] ($SQLcolumns) VALUES ($SQLvalues)" -Verbose
}
Write-Host -NoNewline "Setting feature key $featureKey to $enabledStr - "
$result = Invoke-Sqlcmd -Database $databaseName -Query "UPDATE [dbo].[Tenant Feature Key] set Enabled = $enabled where ID = '$featureKey';Select @@ROWCOUNT"
if ($result[0] -eq "1") {
Write-Host " Success"
}
else {
throw
}
}
catch {
Write-Host " Failure"
Write-Host "WARNING: Unable to set feature key $featureKey"
}
}
}
}
} -argumentList $tenant, $featureKeys
}
}
catch {
TrackException -telemetryScope $telemetryScope -errorRecord $_
throw
}
finally {
TrackTrace -telemetryScope $telemetryScope
}
}
Export-ModuleMember -Function Set-BcContainerFeatureKeys