Skip to content

Commit 55e6b31

Browse files
authored
issue 3369 and 3370 (#3389)
Fixes #3369 Fixes #3370 Issue 3369 Patch images version 1.0.2.15 with an updated installer Add awareness of Windows 11 23H2 Patch images version 1.0.2.15 with an updated prompt.ps1, which loads PowerShell 7 modules when running pwsh Add new setting usePwshForBC24, which is default true to instruct Invoke-ScriptInBcContainer to use pwsh (PS7) instead of powershell (PS5) when invoking scripts in BC 24+ containers Add parameter usePwsh to Open-BcContainer and Invoke-ScriptInBcContainer, defaulted to the value of the usePwshForBC24 setting --------- Co-authored-by: freddydk <[email protected]>
1 parent a200d60 commit 55e6b31

6 files changed

+63
-19
lines changed

BC.HelperFunctions.ps1

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ function Get-ContainerHelperConfig {
1414
"genericImageName" = 'mcr.microsoft.com/businesscentral:{0}'
1515
"genericImageNameFilesOnly" = 'mcr.microsoft.com/businesscentral:{0}-filesonly'
1616
"usePsSession" = $true
17+
"usePwshForBc24" = $true
1718
"tryWinRmSession" = !$isAdministrator
1819
"addTryCatchToScriptBlock" = $true
1920
"killPsSessionProcess" = $false

ContainerHandling/Invoke-ScriptInNavContainer.ps1

+20-4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
A pre-compiled PowerShell scriptblock to invoke
1111
.Parameter argumentList
1212
Arguments to transfer to the scriptblock in form of an object[]
13+
.Parameter useSession
14+
If true, the scriptblock will be invoked in a PowerShell session in the container. If false, the scriptblock will be invoked using docker exec
15+
.Parameter usePwsh
16+
If true, the scriptblock will be invoked using pwsh instead of powershell (when BC version is 24 or later)
1317
.Example
1418
Invoke-ScriptInBcContainer -containerName dev -scriptblock { $env:UserName }
1519
.Example
@@ -22,12 +26,24 @@ function Invoke-ScriptInBcContainer {
2226
[ScriptBlock] $scriptblock,
2327
[Parameter(Mandatory=$false)]
2428
[Object[]] $argumentList,
25-
[bool] $useSession = $bcContainerHelperConfig.usePsSession
29+
[bool] $useSession = $bcContainerHelperConfig.usePsSession,
30+
[bool] $usePwsh = $bccontainerHelperConfig.usePwshForBc24
2631
)
2732

2833
$file = Join-Path $bcContainerHelperConfig.hostHelperFolder ([GUID]::NewGuid().Tostring()+'.ps1')
2934
$containerFile = ""
30-
if (!$useSession) {
35+
$shell = 'powershell'
36+
if ($usePwsh) {
37+
[System.Version]$platformVersion = Get-BcContainerPlatformVersion -containerOrImageName $containerName
38+
if ($platformVersion -ge [System.Version]"24.0.0.0") {
39+
$useSession = $false
40+
$shell = 'pwsh'
41+
}
42+
else {
43+
$usePwsh = $false
44+
}
45+
}
46+
if (-not $usePwsh) {
3147
if ($isInsideContainer) {
3248
$useSession = $true
3349
}
@@ -273,7 +289,7 @@ if ($exception) {
273289
$ErrorActionPreference = "Stop"
274290
#$file | Out-Host
275291
#Get-Content -encoding utf8 -path $file | Out-Host
276-
docker exec $containerName powershell $containerFile | Out-Host
292+
docker exec $containerName $shell $containerFile | Out-Host
277293
if($LASTEXITCODE -ne 0) {
278294
Remove-Item $file -Force -ErrorAction SilentlyContinue
279295
Remove-Item $hostOutputFile -Force -ErrorAction SilentlyContinue
@@ -294,7 +310,7 @@ if ($exception) {
294310
'$result = Invoke-Command -ScriptBlock {' + $scriptblock.ToString() + '} -ArgumentList $argumentList' | Add-Content -Encoding $encoding -Path $file
295311
'if ($result) { [System.Management.Automation.PSSerializer]::Serialize($result) | Set-Content -Encoding utf8 "'+$containerOutputFile+'" }' | Add-Content -Encoding $encoding -Path $file
296312
$ErrorActionPreference = "Stop"
297-
docker exec $containerName powershell $containerFile | Out-Host
313+
docker exec $containerName $shell $containerFile | Out-Host
298314
if($LASTEXITCODE -ne 0) {
299315
Remove-Item $file -Force -ErrorAction SilentlyContinue
300316
Remove-Item $hostOutputFile -Force -ErrorAction SilentlyContinue

ContainerHandling/New-NavContainer.ps1

+9-2
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,10 @@ try {
426426

427427
$isServerHost = $os.ProductType -eq 3
428428

429-
if ($os.BuildNumber -eq 22621) {
429+
if ($os.BuildNumber -eq 22631) {
430+
$hostOs = "23H2"
431+
}
432+
elseif ($os.BuildNumber -eq 22621) {
430433
$hostOs = "22H2"
431434
}
432435
elseif ($os.BuildNumber -eq 22000) {
@@ -1332,7 +1335,7 @@ try {
13321335
Write-Host -ForegroundColor Yellow "WARNING: Using process isolation on Windows Desktop OS with generic image version prior to 1.0.2.4 or NAV/BC versions prior to 15.0, might require you to use HyperV isolation or disable Windows Defender while creating the container"
13331336
}
13341337

1335-
if ($isolation -eq "process" -and !$isServerHost -and $os.BuildNumber -eq 22621 -and $useSSL) {
1338+
if ($isolation -eq "process" -and !$isServerHost -and ($os.BuildNumber -eq 22621 -or $os.BuildNumber -eq 22631) -and $useSSL) {
13361339
Write-Host -ForegroundColor Red "WARNING: Using SSL when running Windows 11 with process isolation might not work due to a bug in Windows 11. Please use HyperV isolation or disable SSL."
13371340
}
13381341

@@ -1754,6 +1757,10 @@ if (Test-Path "c:\run\my\powershell-7.4.1-win-x64.msi") { Write-Host "Installing
17541757
') | Add-Content -Path "$myfolder\HelperFunctions.ps1"
17551758
}
17561759

1760+
if ($version.Major -ge 24 -and $genericTag -eq [System.Version]"1.0.2.15") {
1761+
Download-File -source "https://raw.githubusercontent.com/microsoft/nav-docker/98c0702dbd607580880a3c9248cd76591868447d/generic/Run/Prompt.ps1" -destinationFile (Join-Path $myFolder "Prompt.ps1")
1762+
}
1763+
17571764
if ($version.Major -ge 15 -and $version.Major -le 18 -and $genericTag -ge [System.Version]"1.0.2.15") {
17581765
if (!(Test-Path -Path "$myfolder\HelperFunctions.ps1")) {
17591766
('# Invoke default behavior

ContainerHandling/New-NavImage.ps1

+10-2
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,10 @@ try {
138138
$baseImage = $bestGenericImageName
139139
}
140140

141-
if ($os.BuildNumber -eq 22621) {
141+
if ($os.BuildNumber -eq 22631) {
142+
$hostOs = "23H2"
143+
}
144+
elseif ($os.BuildNumber -eq 22621) {
142145
$hostOs = "22H2"
143146
}
144147
elseif ($os.BuildNumber -eq 22000) {
@@ -327,7 +330,7 @@ try {
327330
$startTime = [DateTime]::Now
328331

329332
if ($populateBuildFolder) {
330-
$genericTag = [Version]"1.0.2.14"
333+
$genericTag = [Version]"1.0.2.15"
331334
}
332335
else {
333336
if ($baseImage -like 'mcr.microsoft.com/businesscentral:*') {
@@ -494,6 +497,11 @@ try {
494497
$InstallDotNet = 'RUN start-process -Wait -FilePath "c:\run\DotNetCore.1.0.7_1.1.4-WindowsHosting.exe" -ArgumentList /quiet'
495498
}
496499

500+
if ($genericTag -eq [Version]"1.0.2.15" -and [Version]$appManifest.Version -ge [Version]"24.0.0.0") {
501+
$myScripts += @( 'https://raw.githubusercontent.com/microsoft/nav-docker/4b8870e6c023c399d309e389bf32fde44fcb1871/generic/Run/240/navinstall.ps1' )
502+
Write-Host "Patching installer from generic image 1.0.2.15"
503+
}
504+
497505
$myScripts | ForEach-Object {
498506
if ($_ -is [string]) {
499507
if ($_.StartsWith("https://", "OrdinalIgnoreCase") -or $_.StartsWith("http://", "OrdinalIgnoreCase")) {

ContainerHandling/Open-NavContainer.ps1

+18-11
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,36 @@
66
The PowerShell prompt will have the PowerShell modules pre-loaded, meaning that you can use most PowerShell CmdLets.
77
.Parameter containerName
88
Name of the container for which you want to open a session
9+
.Parameter usePwsh
10+
If true, the powershell session opened will use pwsh instead of powershell (when BC version is 24 or later)
911
.Example
1012
Open-BcContainer -containerName bcserver
1113
#>
1214
function Open-BcContainer {
1315
[CmdletBinding()]
1416
Param (
15-
[string] $containerName = $bcContainerHelperConfig.defaultContainerName
17+
[string] $containerName = $bcContainerHelperConfig.defaultContainerName,
18+
[bool] $usePwsh = $bccontainerHelperConfig.usePwshForBc24
1619
)
1720

1821
Process {
22+
$shell = 'powershell'
1923
try {
2024
$inspect = docker inspect $containerName | ConvertFrom-Json
2125
$version = [Version]$inspect.Config.Labels.version
22-
$vs = "Business Central"
23-
if ($version.Major -le 14) {
24-
$vs = "NAV"
25-
}
26-
$psPrompt = """function prompt {'[$($containerName.ToUpperInvariant())] PS '+`$executionContext.SessionState.Path.CurrentLocation+('>'*(`$nestedPromptLevel+1))+' '}; Write-Host 'Welcome to the $vs Container PowerShell prompt'; Write-Host 'Microsoft Windows Version $($inspect.Config.Labels.osversion)'; Write-Host 'Windows PowerShell Version $($PSVersionTable.psversion.ToString())'; Write-Host; . 'c:\run\prompt.ps1' -silent"""
27-
}
28-
catch {
29-
$psPrompt = """function prompt {'[$($containerName.ToUpperInvariant())] PS '+`$executionContext.SessionState.Path.CurrentLocation+('>'*(`$nestedPromptLevel+1))+' '}; . 'c:\run\prompt.ps1'"""
30-
}
31-
Start-Process "cmd.exe" @("/C";"docker exec -it $containerName powershell -noexit $psPrompt")
26+
$vs = "Business Central"
27+
if ($version.Major -le 14) {
28+
$vs = "NAV"
29+
}
30+
if ($version.Major -ge 24 -and $usePwsh) {
31+
$shell = 'pwsh'
32+
}
33+
$psPrompt = """function prompt {'[$($containerName.ToUpperInvariant())] PS '+`$executionContext.SessionState.Path.CurrentLocation+('>'*(`$nestedPromptLevel+1))+' '}; Write-Host 'Welcome to the $vs Container PowerShell prompt'; Write-Host 'Microsoft Windows Version $($inspect.Config.Labels.osversion)'; Write-Host ""Windows PowerShell Version `$(`$PSVersionTable.psversion.ToString())""; Write-Host; . 'c:\run\prompt.ps1' -silent"""
34+
}
35+
catch {
36+
$psPrompt = """function prompt {'[$($containerName.ToUpperInvariant())] PS '+`$executionContext.SessionState.Path.CurrentLocation+('>'*(`$nestedPromptLevel+1))+' '}; . 'c:\run\prompt.ps1'"""
37+
}
38+
Start-Process "cmd.exe" @("/C";"docker exec -it $containerName $shell -noexit -command $psPrompt")
3239
}
3340
}
3441
Set-Alias -Name Open-NavContainer -Value Open-BcContainer

ReleaseNotes.txt

+5
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ Issue #3379 Adding -installCertificateOnHost to New-BcContainer didn't work when
99
Issue #3376 Regression - Download-Artifacts stopped downloading pre-requisites
1010
Issue 3371 Unable to get companyname from Windows Users when running tests
1111
Disallow invoking Run-ALPipeline with `keepContainer` without specifying credentials to use
12+
Issue 3369 Patch images version 1.0.2.15 with an updated installer
13+
Add awareness of Windows 11 23H2
14+
Patch images version 1.0.2.15 with an updated prompt.ps1, which loads PowerShell 7 modules when running pwsh
15+
Add new setting usePwshForBC24, which is default true to instruct Invoke-ScriptInBcContainer to use pwsh (PS7) instead of powershell (PS5) when invoking scripts in BC 24+ containers
16+
Add parameter usePwsh to Open-BcContainer and Invoke-ScriptInBcContainer, defaulted to the value of the usePwshForBC24 setting
1217

1318
6.0.6
1419
Include Microsoft_Business Foundation Test Libraries.app when importing test libraries (and tests)

0 commit comments

Comments
 (0)