Skip to content

Commit

Permalink
Encoding issue with PowerShell 7.4.1 (#3324)
Browse files Browse the repository at this point in the history
and a bugfix
Fixes #3322

---------

Co-authored-by: freddydk <freddydk@users.noreply.github.com>
  • Loading branch information
freddydk and freddydk committed Feb 2, 2024
1 parent d00b9a2 commit dc5eee6
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 25 deletions.
4 changes: 2 additions & 2 deletions AppHandling/Publish-NavContainerApp.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ try {
$version = [System.Version]($navversion.split('-')[0])
$force = ($version.Major -ge 14)
if ($checkAlreadyInstalled) {
# Get Installed apps (if UseDevEndpoint is specified, only get global apps)
$installedApps = Get-BcContainerAppInfo -containerName $containerName -installedOnly | Where-Object { (-not $useDevEndpoint.IsPresent) -or ($_.Scope -eq 'Global') } | ForEach-Object {
# Get Installed apps (if UseDevEndpoint is specified, only get global apps)
$installedApps = Get-BcContainerAppInfo -containerName $containerName -installedOnly | Where-Object { (-not $useDevEndpoint.IsPresent) -or ($_.Scope -eq 'Global') } | ForEach-Object {
@{ "id" = $_.appId; "publisher" = $_.publisher; "name" = $_.name; "version" = $_.Version }
}
}
Expand Down
1 change: 1 addition & 0 deletions Artifacts/Get-BCArtifactUrl.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ try {
$nextminorversion = $nextmajorversion
}

if (-not $country) { $country = 'w1' }
$insiders = Get-BcArtifactUrl -country $country -storageAccount bcinsider -select All -sasToken $sasToken -doNotCheckPlatform:$doNotCheckPlatform -accept_insiderEula:$accept_insiderEula
$nextmajor = $insiders | Where-Object { $_.Split('/')[4].StartsWith($nextmajorversion) } | Select-Object -Last 1
$nextminor = $insiders | Where-Object { $_.Split('/')[4].StartsWith($nextminorversion) } | Select-Object -Last 1
Expand Down
50 changes: 27 additions & 23 deletions ContainerHandling/Invoke-ScriptInNavContainer.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ function Invoke-ScriptInBcContainer {
$hostOutputFile = "$file.output"
$containerOutputFile = "$containerFile.output"
try {
$oldEncoding = [Console]::OutputEncoding
try { [Console]::OutputEncoding = [System.Text.Encoding]::UTF8 } catch {}
if ($isPsCore) { $encoding = 'UTF8BOM' } else { $encoding = 'UTF8' }
if ($argumentList) {
$encryptionKey = $null
$xml = [xml]([System.Management.Automation.PSSerializer]::Serialize($argumentList))
Expand All @@ -127,23 +130,23 @@ function Invoke-ScriptInBcContainer {
if ($nodes.Count -gt 0) {
$encryptionKey = New-Object Byte[] 16
[Security.Cryptography.RNGCryptoServiceProvider]::Create().GetBytes($encryptionKey)
'$encryptionkey = [System.Management.Automation.PSSerializer]::Deserialize('''+([xml]([System.Management.Automation.PSSerializer]::Serialize($encryptionKey))).OuterXml+''')' | Add-Content $file
'$encryptionkey = [System.Management.Automation.PSSerializer]::Deserialize('''+([xml]([System.Management.Automation.PSSerializer]::Serialize($encryptionKey))).OuterXml+''')' | Add-Content -Encoding $encoding -Path $file
}
foreach($node in $nodes) {
$node.InnerText = ConvertFrom-SecureString -SecureString ($node.InnerText | ConvertTo-SecureString) -Key $encryptionkey
}

$xmlbytes =[System.Text.Encoding]::UTF8.GetBytes($xml.OuterXml)
'$xmlbytes = [Convert]::FromBase64String('''+[Convert]::ToBase64String($xmlbytes)+''')' | Add-Content $file
'$xml = [xml]([System.Text.Encoding]::UTF8.GetString($xmlbytes))' | Add-Content $file
'$xmlbytes = [Convert]::FromBase64String('''+[Convert]::ToBase64String($xmlbytes)+''')' | Add-Content -Encoding $encoding -Path $file
'$xml = [xml]([System.Text.Encoding]::UTF8.GetString($xmlbytes))' | Add-Content -Encoding $encoding -Path $file

if ($encryptionKey) {
'$nsmgr = New-Object System.Xml.XmlNamespaceManager -ArgumentList $xml.NameTable' | Add-Content $file
'$nsmgr.AddNamespace("ns", "http://schemas.microsoft.com/powershell/2004/04")' | Add-Content $file
'$nodes = $xml.SelectNodes("//ns:SS", $nsmgr)' | Add-Content $file
'foreach($node in $nodes) { $node.InnerText = ConvertFrom-SecureString -SecureString ($node.InnerText | ConvertTo-SecureString -Key $encryptionKey) }' | Add-Content $file
'$nsmgr = New-Object System.Xml.XmlNamespaceManager -ArgumentList $xml.NameTable' | Add-Content -Encoding $encoding -Path $file
'$nsmgr.AddNamespace("ns", "http://schemas.microsoft.com/powershell/2004/04")' | Add-Content -Encoding $encoding -Path $file
'$nodes = $xml.SelectNodes("//ns:SS", $nsmgr)' | Add-Content -Encoding $encoding -Path $file
'foreach($node in $nodes) { $node.InnerText = ConvertFrom-SecureString -SecureString ($node.InnerText | ConvertTo-SecureString -Key $encryptionKey) }' | Add-Content -Encoding $encoding -Path $file
}
'$argumentList = [System.Management.Automation.PSSerializer]::Deserialize($xml.OuterXml)' | Add-Content $file
'$argumentList = [System.Management.Automation.PSSerializer]::Deserialize($xml.OuterXml)' | Add-Content -Encoding $encoding -Path $file
}

'$runPath = "c:\Run"
Expand Down Expand Up @@ -175,13 +178,13 @@ if ($roleTailoredClientFolder) {
Set-Location $runPath
$ErrorActionPreference = "Stop"
$startTime = [DateTime]::Now
' | Add-Content $file
' | Add-Content -Encoding $encoding -Path $file

"`$WarningPreference = '$($bcContainerHelperConfig.WarningPreference)'
" | Add-Content $file
" | Add-Content -Encoding $encoding -Path $file

"`$containerName = '$containerName'
" | Add-Content $file
" | Add-Content -Encoding $encoding -Path $file

if ($bcContainerHelperConfig.addTryCatchToScriptBlock) {
$ast = $scriptblock.Ast
Expand All @@ -192,19 +195,19 @@ $startTime = [DateTime]::Now
if ($ast.ParamBlock) {
$script = $ast.Extent.text.Replace($ast.ParamBlock.Extent.Text,'').Trim()
if ($script.StartsWith('{')) {
"`$result = Invoke-Command -ScriptBlock { $($ast.ParamBlock.Extent.Text) try $script catch { ""::EXCEPTION::`$(`$_.Exception.Message)"" } } -ArgumentList `$argumentList" | Add-Content $file
"`$result = Invoke-Command -ScriptBlock { $($ast.ParamBlock.Extent.Text) try $script catch { ""::EXCEPTION::`$(`$_.Exception.Message)"" } } -ArgumentList `$argumentList" | Add-Content -Encoding $encoding -Path $file
}
else {
"`$result = Invoke-Command -ScriptBlock { $($ast.ParamBlock.Extent.Text) try { $script } catch { ""::EXCEPTION::`$(`$_.Exception.Message)"" } } -ArgumentList `$argumentList" | Add-Content $file
"`$result = Invoke-Command -ScriptBlock { $($ast.ParamBlock.Extent.Text) try { $script } catch { ""::EXCEPTION::`$(`$_.Exception.Message)"" } } -ArgumentList `$argumentList" | Add-Content -Encoding $encoding -Path $file
}
}
else {
$script = $ast.Extent.text.Trim()
if ($script.StartsWith('{')) {
"`$result = Invoke-Command -ScriptBlock { try $($ast.Extent.text) catch { ""::EXCEPTION::`$(`$_.Exception.Message)"" } }" | Add-Content $file
"`$result = Invoke-Command -ScriptBlock { try $($ast.Extent.text) catch { ""::EXCEPTION::`$(`$_.Exception.Message)"" } }" | Add-Content -Encoding $encoding -Path $file
}
else {
"`$result = Invoke-Command -ScriptBlock { try { $($ast.Extent.text) } catch { ""::EXCEPTION::`$(`$_.Exception.Message)"" } }" | Add-Content $file
"`$result = Invoke-Command -ScriptBlock { try { $($ast.Extent.text) } catch { ""::EXCEPTION::`$(`$_.Exception.Message)"" } }" | Add-Content -Encoding $encoding -Path $file
}
}
}
Expand Down Expand Up @@ -261,15 +264,15 @@ if ($exception) {
$result = @("::EXCEPTION::$errorMessage") + @($result | Where-Object { $_ -notlike "::EXCEPTION::*" })
}
'@ | Add-Content $file
'@ | Add-Content -Encoding $encoding -Path $file

'if ($result -ne $null) { [System.Management.Automation.PSSerializer]::Serialize($result) | Set-Content "'+$containerOutputFile+'" }' | Add-Content $file
'if ($result -ne $null) { [System.Management.Automation.PSSerializer]::Serialize($result) | Set-Content -Encoding utf8 "'+$containerOutputFile+'" }' | Add-Content -Encoding $encoding -Path $file

#Write-Host -ForegroundColor cyan (Get-Content $file -Raw -Encoding UTF8)

$ErrorActionPreference = "Stop"
# $file | Out-Host
# Get-Content -path $file | Out-Host
#$file | Out-Host
#Get-Content -encoding utf8 -path $file | Out-Host
docker exec $containerName powershell $containerFile | Out-Host
if($LASTEXITCODE -ne 0) {
Remove-Item $file -Force -ErrorAction SilentlyContinue
Expand All @@ -278,7 +281,7 @@ if ($exception) {
}
if (Test-Path -Path $hostOutputFile -PathType Leaf) {
# Write-Host -ForegroundColor Cyan "'$(Get-content $hostOutputFile -Raw -Encoding UTF8)'"
$result = [System.Management.Automation.PSSerializer]::Deserialize((Get-content $hostOutputFile))
$result = [System.Management.Automation.PSSerializer]::Deserialize((Get-content -encoding utf8 $hostOutputFile))
$exception = $result | Where-Object { $_ -like "::EXCEPTION::*" }
if ($exception) {
$errorMessage = $exception.SubString(13)
Expand All @@ -288,8 +291,8 @@ if ($exception) {
}
}
else {
'$result = Invoke-Command -ScriptBlock {' + $scriptblock.ToString() + '} -ArgumentList $argumentList' | Add-Content $file
'if ($result) { [System.Management.Automation.PSSerializer]::Serialize($result) | Set-Content "'+$containerOutputFile+'" }' | Add-Content $file
'$result = Invoke-Command -ScriptBlock {' + $scriptblock.ToString() + '} -ArgumentList $argumentList' | Add-Content -Encoding $encoding -Path $file
'if ($result) { [System.Management.Automation.PSSerializer]::Serialize($result) | Set-Content -Encoding utf8 "'+$containerOutputFile+'" }' | Add-Content -Encoding $encoding -Path $file
$ErrorActionPreference = "Stop"
docker exec $containerName powershell $containerFile | Out-Host
if($LASTEXITCODE -ne 0) {
Expand All @@ -298,10 +301,11 @@ if ($exception) {
throw "Error executing script in Container"
}
if (Test-Path -Path $hostOutputFile -PathType Leaf) {
[System.Management.Automation.PSSerializer]::Deserialize((Get-content $hostOutputFile))
[System.Management.Automation.PSSerializer]::Deserialize((Get-content -encoding utf8 $hostOutputFile))
}
}
} finally {
try { [Console]::OutputEncoding = $oldEncoding } catch {}
Remove-Item $file -Force -ErrorAction SilentlyContinue
Remove-Item $hostOutputFile -Force -ErrorAction SilentlyContinue
}
Expand Down
2 changes: 2 additions & 0 deletions ReleaseNotes.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ Issue 3313 Strange error in Copy-BcEnvironment
Issue 3318 Download-BcNuGetPackageToFolder fails when no dependencies defined
Add Stacktrace output to high level functions Download-BcNuGetPackageToFolder and Get-BcNuGetPackage
Fix for AL-Go issue 913 (https://github.com/microsoft/AL-Go/issues/913)
Issue 3322 Default country for nextminor and nextmajor should be w1, not whatever country was the last one generated
Encoding problems when using Invoke-ScriptInBcContainer with PowerShell 7.4.1

6.0.4
Rename AppInfoCache.json to cache_AppInfo.json (which by default is covered by .gitignore in AL-Go for GitHub)
Expand Down

0 comments on commit dc5eee6

Please sign in to comment.