Skip to content

Commit cb965cb

Browse files
authored
Issue3473 (#3505)
Fixes #3473 + allowing mixed sessions (PS5/PS7) by not reusing cached session + do not pre-initialize a container session + speed up invoke-script --------- Co-authored-by: freddydk <[email protected]>
1 parent 28af69c commit cb965cb

8 files changed

+66
-68
lines changed

AppHandling/Clean-BcContainerDatabase.ps1

+1-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ try {
9696
$copyTables += @("Entitlement", "Entitlement Set", "Membership Entitlement")
9797
}
9898

99-
Invoke-ScriptInBCContainer -containerName $containerName -scriptblock { Param($platformVersion, $databaseName, $databaseServer, $databaseInstance, $copyTables, $multitenant)
99+
Invoke-ScriptInBCContainer -containerName $containerName -useSession $false -usePwsh $false -scriptblock { Param($platformVersion, $databaseName, $databaseServer, $databaseInstance, $copyTables, $multitenant)
100100

101101
Write-Host "Stopping ServiceTier in order to replace database"
102102
Set-NavServerInstance -ServerInstance $ServerInstance -stop

AzureAD/New-AadAppsForBc.ps1

+6-5
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,13 @@ try {
8282
$accessToken = $bcAuthContext.accessToken
8383
}
8484
if ($accessToken) {
85-
try {
86-
# Connect-MgGraph changed type of accesstoken parameter from plain text to securestring along the way
87-
Connect-MgGraph -accessToken (ConvertTo-SecureString -String $accessToken -AsPlainText -Force) | Out-Null
85+
# Check the AccessToken since Microsoft Graph V2 requires a SecureString
86+
$graphAccesTokenParameter = (Get-Command Connect-MgGraph).Parameters['AccessToken']
87+
if ($graphAccesTokenParameter.ParameterType -eq [securestring]) {
88+
Connect-MgGraph -AccessToken (ConvertTo-SecureString -String $accessToken -AsPlainText -Force) | Out-Null
8889
}
89-
catch [System.ArgumentException] {
90-
Connect-MgGraph -accessToken $accessToken | Out-Null
90+
else {
91+
Connect-MgGraph -AccessToken $accessToken | Out-Null
9192
}
9293
}
9394
else {

AzureAD/Remove-AadAppsForBc.ps1

+3-8
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,6 @@ try {
3939
Install-Package Microsoft.Graph -Force -WarningAction Ignore | Out-Null
4040
}
4141

42-
# Check the AccessToken since Microsoft Graph V2 requires a SecureString
43-
$graphAccesTokenParameter = (Get-Command Connect-MgGraph).Parameters['AccessToken']
44-
45-
if ($graphAccesTokenParameter.ParameterType -eq [securestring]){
46-
$useSecureStringForAccessToken = $true
47-
}
48-
4942
# Connect to Microsoft.Graph
5043
if (!$useCurrentMicrosoftGraphConnection) {
5144
if ($bcAuthContext) {
@@ -57,7 +50,9 @@ try {
5750
$accessToken = $bcAuthContext.accessToken
5851
}
5952
if ($accessToken) {
60-
if ($useSecureStringForAccessToken){
53+
# Check the AccessToken since Microsoft Graph V2 requires a SecureString
54+
$graphAccesTokenParameter = (Get-Command Connect-MgGraph).Parameters['AccessToken']
55+
if ($graphAccesTokenParameter.ParameterType -eq [securestring]) {
6156
Connect-MgGraph -AccessToken (ConvertTo-SecureString -String $accessToken -AsPlainText -Force) | Out-Null
6257
}
6358
else {

Bacpac/Export-NavContainerDatabasesAsBacpac.ps1

+36-30
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
Include this parameter to avoid checking entitlements. Entitlements are needed if the .bacpac file is to be used for cloud deployments.
1818
.Parameter includeDacPac
1919
Use this parameter to export databases as dacpac
20+
.Parameter dacPacOnly
21+
Use this parameter to export databases as dacpac only (skip Bacpac)
2022
.Parameter commandTimeout
2123
Timeout in seconds for the export command for every database. Default is 1 hour (3600).
2224
.Parameter diagnostics
@@ -43,6 +45,7 @@ function Export-BcContainerDatabasesAsBacpac {
4345
[string[]] $tenant = @("default"),
4446
[int] $commandTimeout = 3600,
4547
[switch] $includeDacPac,
48+
[switch] $dacPacOnly,
4649
[switch] $diagnostics,
4750
[switch] $doNotCheckEntitlements,
4851
[string[]] $additionalArguments = @()
@@ -64,7 +67,7 @@ try {
6467
}
6568
$containerBacpacFolder = Get-BcContainerPath -containerName $containerName -path $bacpacFolder -throw
6669

67-
Invoke-ScriptInBcContainer -containerName $containerName -ScriptBlock { Param([PSCredential]$sqlCredential, $bacpacFolder, $tenant, $commandTimeout, $includeDacPac, $diagnostics, $additionalArguments, $doNotCheckEntitlements)
70+
Invoke-ScriptInBcContainer -containerName $containerName -ScriptBlock { Param([PSCredential]$sqlCredential, $bacpacFolder, $tenant, $commandTimeout, $includeDacPac, $dacPacOnly, $diagnostics, $additionalArguments, $doNotCheckEntitlements)
6871

6972
function CmdDo {
7073
Param(
@@ -388,40 +391,43 @@ try {
388391
[Parameter(Mandatory=$false)]
389392
[int] $commandTimeout = 3600,
390393
[switch] $includeDacPac,
394+
[switch] $dacPacOnly,
391395
[switch] $diagnostics,
392396
[Parameter(Mandatory=$false)]
393397
[string[]] $additionalArguments = @()
394398
)
395399

396-
Write-Host "Exporting as BacPac..."
400+
if (!$dacPacOnly) {
401+
Write-Host "Exporting as BacPac..."
397402

398-
$arguments = @(
399-
('/Action:Export'),
400-
('/TargetFile:"'+$targetFile+'"'),
401-
('/SourceDatabaseName:"'+$databaseName+'"'),
402-
('/SourceServerName:"'+$databaseServer+'"'),
403-
('/OverwriteFiles:True')
404-
("/p:CommandTimeout=$commandTimeout")
405-
)
406-
407-
if ($diagnostics) {
408-
$arguments += @('/Diagnostics:True')
409-
}
410-
411-
if ($sqlCredential) {
412-
$arguments += @(
413-
('/SourceUser:"'+$sqlCredential.UserName+'"'),
414-
('/SourcePassword:"'+([System.Runtime.InteropServices.Marshal]::PtrToStringAuto([System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($sqlCredential.Password)))+'"')
403+
$arguments = @(
404+
('/Action:Export'),
405+
('/TargetFile:"'+$targetFile+'"'),
406+
('/SourceDatabaseName:"'+$databaseName+'"'),
407+
('/SourceServerName:"'+$databaseServer+'"'),
408+
('/OverwriteFiles:True')
409+
("/p:CommandTimeout=$commandTimeout")
415410
)
411+
412+
if ($diagnostics) {
413+
$arguments += @('/Diagnostics:True')
414+
}
415+
416+
if ($sqlCredential) {
417+
$arguments += @(
418+
('/SourceUser:"'+$sqlCredential.UserName+'"'),
419+
('/SourcePassword:"'+([System.Runtime.InteropServices.Marshal]::PtrToStringAuto([System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($sqlCredential.Password)))+'"')
420+
)
421+
}
422+
423+
if ($additionalArguments) {
424+
$arguments += $additionalArguments
425+
}
426+
427+
CmdDo -command $sqlpackageExe -arguments ($arguments -join ' ')
416428
}
417-
418-
if ($additionalArguments) {
419-
$arguments += $additionalArguments
420-
}
421-
422-
CmdDo -command $sqlpackageExe -arguments ($arguments -join ' ')
423429

424-
if ($includeDacPac) {
430+
if ($includeDacPac -or $dacPacOnly) {
425431
Write-Host "Extracting as DacPac..."
426432
$arguments = @(
427433
('/Action:Extract'),
@@ -481,7 +487,7 @@ try {
481487
Remove-WindowsUsers -DatabaseServer $databaseServerInstance -DatabaseName $tempAppDatabaseName -sqlCredential $sqlCredential
482488
Remove-ApplicationRoles -DatabaseServer $databaseServerInstance -DatabaseName $tempAppDatabaseName -sqlCredential $sqlCredential
483489
Remove-NavDatabaseSystemTableData -DatabaseServer $databaseServerInstance -DatabaseName $tempAppDatabaseName -sqlCredential $sqlCredential
484-
Do-Export -DatabaseServer $databaseServerInstance -DatabaseName $tempAppDatabaseName -sqlCredential $sqlCredential -targetFile $appBacpacFileName -commandTimeout $commandTimeout -includeDacPac:$includeDacPac -diagnostics:$diagnostics -additionalArguments $additionalArguments
490+
Do-Export -DatabaseServer $databaseServerInstance -DatabaseName $tempAppDatabaseName -sqlCredential $sqlCredential -targetFile $appBacpacFileName -commandTimeout $commandTimeout -includeDacPac:$includeDacPac -dacPacOnly:$dacPacOnly -diagnostics:$diagnostics -additionalArguments $additionalArguments
485491

486492
$tenant | ForEach-Object {
487493
$sourceDatabase = $_
@@ -499,7 +505,7 @@ try {
499505
Remove-WindowsUsers -DatabaseServer $databaseServerInstance -DatabaseName $tempTenantDatabaseName -sqlCredential $sqlCredential
500506
Remove-ApplicationRoles -DatabaseServer $databaseServerInstance -DatabaseName $tempTenantDatabaseName -sqlCredential $sqlCredential
501507
Remove-NavTenantDatabaseUserData -DatabaseServer $databaseServerInstance -DatabaseName $tempTenantDatabaseName -sqlCredential $sqlCredential
502-
Do-Export -DatabaseServer $databaseServerInstance -DatabaseName $tempTenantDatabaseName -sqlCredential $sqlCredential -targetFile $tenantBacpacFileName -commandTimeout $commandTimeout -includeDacPac:$includeDacPac -diagnostics:$diagnostics -additionalArguments $additionalArguments
508+
Do-Export -DatabaseServer $databaseServerInstance -DatabaseName $tempTenantDatabaseName -sqlCredential $sqlCredential -targetFile $tenantBacpacFileName -commandTimeout $commandTimeout -includeDacPac:$includeDacPac -dacPacOnly:$dacPacOnly -diagnostics:$diagnostics -additionalArguments $additionalArguments
503509
}
504510
} else {
505511
$tempDatabaseName = "temp$DatabaseName"
@@ -512,9 +518,9 @@ try {
512518
Remove-ApplicationRoles -DatabaseServer $databaseServerInstance -DatabaseName $tempDatabaseName -sqlCredential $sqlCredential
513519
Remove-NavDatabaseSystemTableData -DatabaseServer $databaseServerInstance -DatabaseName $tempDatabaseName -sqlCredential $sqlCredential
514520
Remove-NavTenantDatabaseUserData -DatabaseServer $databaseServerInstance -DatabaseName $tempDatabaseName -sqlCredential $sqlCredential
515-
Do-Export -DatabaseServer $databaseServerInstance -DatabaseName $tempDatabaseName -sqlCredential $sqlCredential -targetFile $bacpacFileName -commandTimeout $commandTimeout -includeDacPac:$includeDacPac -diagnostics:$diagnostics -additionalArguments $additionalArguments
521+
Do-Export -DatabaseServer $databaseServerInstance -DatabaseName $tempDatabaseName -sqlCredential $sqlCredential -targetFile $bacpacFileName -commandTimeout $commandTimeout -includeDacPac:$includeDacPac -dacPacOnly:$dacPacOnly -diagnostics:$diagnostics -additionalArguments $additionalArguments
516522
}
517-
} -ArgumentList $sqlCredential, $containerBacpacFolder, $tenant, $commandTimeout, $includeDacPac, $diagnostics, $additionalArguments, $doNotCheckEntitlements
523+
} -ArgumentList $sqlCredential, $containerBacpacFolder, $tenant, $commandTimeout, $includeDacPac, $dacPacOnly, $diagnostics, $additionalArguments, $doNotCheckEntitlements
518524
}
519525
catch {
520526
TrackException -telemetryScope $telemetryScope -errorRecord $_

ContainerHandling/Get-NavContainerSession.ps1

+13-3
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,18 @@ function Get-BcContainerSession {
2929
if ($sessions.ContainsKey($containerName)) {
3030
$session = $sessions[$containerName]
3131
try {
32-
Invoke-Command -Session $session -ScriptBlock { $true } | Out-Null
33-
if (!$reinit) { return $session }
32+
$platformVersion = Invoke-Command -Session $session -ScriptBlock { [System.Version](get-item 'C:\Program Files\Microsoft Dynamics NAV\*\Service\Microsoft.Dynamics.Nav.Server.exe').Versioninfo.FileVersion }
33+
if ($platformVersion -ge 24 -and ($usePwsh -xor $session.ConfigurationName -eq 'PowerShell.7')) {
34+
# Cannot use existing session
35+
Remove-PSSession -Session $session
36+
$sessions.Remove($containerName)
37+
$session = $null
38+
}
39+
else {
40+
if (!$reinit) {
41+
return $session
42+
}
43+
}
3444
}
3545
catch {
3646
Remove-PSSession -Session $session
@@ -40,7 +50,7 @@ function Get-BcContainerSession {
4050
}
4151
if (!$session) {
4252
[System.Version]$platformVersion = Get-BcContainerPlatformVersion -containerOrImageName $containerName
43-
if ($platformVersion -lt [System.Version]"24.0.0.0") {
53+
if ($platformVersion.Major -lt 24) {
4454
$usePwsh = $false
4555
}
4656
$configurationName = 'Microsoft.PowerShell'

ContainerHandling/Invoke-ScriptInNavContainer.ps1

+6-4
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,12 @@ function Invoke-ScriptInBcContainer {
3030
[bool] $usePwsh = $bccontainerHelperConfig.usePwshForBc24
3131
)
3232

33-
$file = Join-Path $bcContainerHelperConfig.hostHelperFolder ([GUID]::NewGuid().Tostring()+'.ps1')
34-
$containerFile = $containerFile = Get-BcContainerPath -containerName $containerName -path $file
35-
if ($isInsideContainer -or "$containerFile" -eq "") {
36-
$useSession = $true
33+
if (!$useSession) {
34+
$file = Join-Path $bcContainerHelperConfig.hostHelperFolder ([GUID]::NewGuid().Tostring()+'.ps1')
35+
$containerFile = $containerFile = Get-BcContainerPath -containerName $containerName -path $file
36+
if ($isInsideContainer -or "$containerFile" -eq "") {
37+
$useSession = $true
38+
}
3739
}
3840

3941
if ($useSession) {

ContainerHandling/Wait-NavContainerReady.ps1

-16
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,6 @@ function Wait-BcContainerReady {
4242
throw "Initialization of container $containerName failed"
4343
}
4444

45-
if ($isAdministrator -and $bcContainerHelperConfig.usePsSession -and $cnt -eq ($timeout-30)) {
46-
try {
47-
if (!$sessions.ContainsKey($containerName)) {
48-
$containerId = Get-BcContainerId -containerName $containerName
49-
$session = New-PSSession -ContainerId $containerId -RunAsAdministrator
50-
$sessions.Add($containerName, $session)
51-
}
52-
} catch {}
53-
}
54-
5545
if ($cnt % 5 -eq 0) {
5646
$inspect = docker inspect $containerName | ConvertFrom-Json
5747
if ($inspect.State.Status -eq "exited") {
@@ -63,11 +53,6 @@ function Wait-BcContainerReady {
6353

6454
} while (!($log.Contains("Ready for connections!")))
6555
Write-Host
66-
if ($bcContainerHelperConfig.usePsSession) {
67-
try {
68-
Get-BcContainerSession -containerName $containerName -reinit -silent | Out-Null
69-
} catch {}
70-
}
7156

7257
Invoke-ScriptInBcContainer -containerName $containerName -scriptblock {
7358
$boo = $true
@@ -76,7 +61,6 @@ function Wait-BcContainerReady {
7661
Start-Sleep -Seconds 1
7762
}
7863
}
79-
8064
}
8165
}
8266
Set-Alias -Name Wait-NavContainerReady -Value Wait-BcContainerReady

TenantHandling/New-NavContainerTenant.ps1

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ try {
4646
throw "You cannot add a tenant called tenant"
4747
}
4848

49-
Invoke-ScriptInBcContainer -containerName $containerName -ScriptBlock { Param($containerName, $tenantId, [PSCredential]$sqlCredential, $sourceDatabase, $destinationDatabase, $alternateId, $doNotCopyDatabase, $allowAppDatabaseWrite, $applicationInsightsKey)
49+
Invoke-ScriptInBcContainer -containerName $containerName -useSession $false -usePwsh $false -ScriptBlock { Param($containerName, $tenantId, [PSCredential]$sqlCredential, $sourceDatabase, $destinationDatabase, $alternateId, $doNotCopyDatabase, $allowAppDatabaseWrite, $applicationInsightsKey)
5050

5151
$customConfigFile = Join-Path (Get-Item "C:\Program Files\Microsoft Dynamics NAV\*\Service").FullName "CustomSettings.config"
5252
[xml]$customConfig = [System.IO.File]::ReadAllText($customConfigFile)

0 commit comments

Comments
 (0)