Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhancement of Misc/Set-BcContainerFeatureKeys.ps1 to be able to Enable Feature key for particular company. #3696

Open
wants to merge 19 commits into
base: main
Choose a base branch
from
Open
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 40 additions & 9 deletions Misc/Set-BcContainerFeatureKeys.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<#
<#
.Synopsis
Set Feature Keys in container
.Description
Expand All @@ -9,8 +9,11 @@
Tenant in which you want to set feature keys
.Parameter featureKeys
Hashtable of featureKeys you want to set
.Parameter EnableInCompany
Enables/disables features for given company name. No data update is initiated.
.Example
Set-BcContainerFeatureKeys -containerName test2 -featureKeys @{"EmailHandlingImprovements" = "None"}
Set-BcContainerFeatureKeys -containerName test2 -featureKeys @{"EmailHandlingImprovements" = "None"} -EnableInCompany 'CRONUS International Ltd.'
#>
function Set-BcContainerFeatureKeys {
Param (
Expand All @@ -19,14 +22,16 @@ function Set-BcContainerFeatureKeys {
[Parameter(Mandatory=$false)]
[string] $tenant = "*",
[Parameter(Mandatory=$true)]
[hashtable] $featureKeys
[hashtable] $featureKeys,
[Parameter(Mandatory=$false)]
[string] $EnableInCompany
)

$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)
Invoke-ScriptInBCContainer -containerName $containerName -ScriptBlock { Param([string] $tenant, [hashtable] $featureKeys, $EnableInCompany)
$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
Expand Down Expand Up @@ -55,6 +60,15 @@ try {
$databases | % {
$databaseName = $_
Write-Host "Setting feature keys on database: $databaseName"
# Just information about 'mode' of Feature Key update
if ([String]::IsNullOrEmpty($EnableInCompany))
{
Write-Host "Setting feature keys globally, but not for any company" -ForegroundColor Yellow
}
else
{
Write-Host "Setting feature keys globally and for company "$EnableInCompany -ForegroundColor Yellow
}
$featureKeys.Keys | % {
$featureKey = $_
$enabledStr = $featureKeys[$featureKey]
Expand All @@ -68,20 +82,37 @@ try {
$enabled = -1
Write-Host "WARNING: Unknown value ($enabledStr) for feature key $featureKey"
}
if ($enabled -ne -1) {

# Test if feature which has to be updated is available in table "Feature Data Update Status$63ca2fa4-4f03-4f2b-a480-172fef340d3f"
$FeatureExistsInDestination = Invoke-Sqlcmd -Database $databaseName -Query $("SELECT COUNT(*) FROM [dbo].[Feature Data Update Status"+'$'+"63ca2fa4-4f03-4f2b-a480-172fef340d3f] where [Feature Key] = '$featureKey'")

if(($FeatureExistsInDestination[0].ToString()) -eq "0")
{
KM-JAD marked this conversation as resolved.
Show resolved Hide resolved
Write-host "Feature $featureKey doesn't exist in database"
}

# Feature key is updated just in case that status is correct and respective feature is available in table
if (($enabled -ne -1) -and ($FeatureExistsInDestination[0].ToString() -ne "0")){
try {
#Create new record in table of feature setup table [Tenant Feature Key] in case it is missing
#Create new record in 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'"
Write-host "Creating record for feature $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") {

# Update record in table "Feature Data Update Status$63ca2fa4-4f03-4f2b-a480-172fef340d3f" if it is requested for particular company
$result2 = ''
if (![String]::IsNullOrEmpty($EnableInCompany))
{
$result2 = Invoke-Sqlcmd -Database $databaseName -Query $("UPDATE [dbo].[Feature Data Update Status"+'$'+"63ca2fa4-4f03-4f2b-a480-172fef340d3f] set [Feature Status] = $enabled where [Feature Key] = '$featureKey' AND [Company Name] = '$EnableInCompany';Select @@ROWCOUNT")
}
if (($result[0] -eq "1") -and ((($result2[0] -eq "1") -and ![String]::IsNullOrEmpty($EnableInCompany)) -or ([String]::IsNullOrEmpty($EnableInCompany)))) {
freddydk marked this conversation as resolved.
Show resolved Hide resolved
Write-Host " Success"
}
else {
Expand All @@ -95,7 +126,7 @@ try {
}
}
}
} -argumentList $tenant, $featureKeys
} -argumentList $tenant, $featureKeys, $EnableInCompany
}
}
catch {
Expand Down