Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -4527,6 +4527,14 @@ static InitialSessionState()
ScopedItemOptions.None,
new ArgumentTypeConverterAttribute(typeof(System.Text.Encoding))),

// Variable which controls the encoding for decoding data from a NativeCommand
new SessionStateVariableEntry(
SpecialVariables.PSApplicationOutputEncoding,
null,
RunspaceInit.PSApplicationOutputEncodingDescription,
ScopedItemOptions.None,
new ArgumentTypeConverterAttribute(typeof(Encoding))),

// Preferences
//
// NTRAID#Windows Out Of Band Releases-931461-2006/03/13
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1645,16 +1645,17 @@ private ProcessStartInfo GetProcessStartInfo(
startInfo.UseShellExecute = false;
startInfo.RedirectStandardInput = redirectInput;

Encoding outputEncoding = GetOutputEncoding();
if (redirectOutput)
{
startInfo.RedirectStandardOutput = true;
startInfo.StandardOutputEncoding = Console.OutputEncoding;
startInfo.StandardOutputEncoding = outputEncoding;
}

if (redirectError)
{
startInfo.RedirectStandardError = true;
startInfo.StandardErrorEncoding = Console.OutputEncoding;
startInfo.StandardErrorEncoding = outputEncoding;
}
}

Expand Down Expand Up @@ -1712,6 +1713,20 @@ private ProcessStartInfo GetProcessStartInfo(
return startInfo;
}

#nullable enable
/// <summary>
/// Gets the encoding to use for a process' output/error pipes.
/// </summary>
/// <returns>The encoding to use for the process output.</returns>
private Encoding GetOutputEncoding()
{
Encoding? applicationOutputEncoding = Context.GetVariableValue(
SpecialVariables.PSApplicationOutputEncodingVarPath) as Encoding;

return applicationOutputEncoding ?? Console.OutputEncoding;
}
#nullable disable

/// <summary>
/// Determine if we have a special file which will change the way native argument passing
/// is done on Windows. We use legacy behavior for cmd.exe, .bat, .cmd files.
Expand Down
5 changes: 5 additions & 0 deletions src/System.Management.Automation/engine/SpecialVariables.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ internal static class SpecialVariables

internal static readonly VariablePath OutputEncodingVarPath = new VariablePath(OutputEncoding);

internal const string PSApplicationOutputEncoding = nameof(PSApplicationOutputEncoding);

internal static readonly VariablePath PSApplicationOutputEncodingVarPath = new VariablePath(PSApplicationOutputEncoding);

internal const string VerboseHelpErrors = "VerboseHelpErrors";

internal static readonly VariablePath VerboseHelpErrorsVarPath = new VariablePath(VerboseHelpErrors);
Expand Down Expand Up @@ -388,6 +392,7 @@ internal static class SpecialVariables
SpecialVariables.NestedPromptLevel,
SpecialVariables.pwd,
SpecialVariables.Matches,
SpecialVariables.PSApplicationOutputEncoding,
},
StringComparer.OrdinalIgnoreCase
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,9 @@
<data name="OutputEncodingDescription" xml:space="preserve">
<value>The text encoding used when piping text to a native executable file</value>
</data>
<data name="PSApplicationOutputEncodingDescription" xml:space="preserve">
<value>The text encoding used when reading output text from a native executable file</value>
</data>
<data name="PSStyleDescription" xml:space="preserve">
<value>Configuration controlling how text is rendered.</value>
</data>
Expand Down
62 changes: 62 additions & 0 deletions test/powershell/engine/Basic/NativeCommandEncoding.Tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.

Describe 'Native command output encoding tests' -Tags 'CI' {
BeforeAll {
$WriteConsoleOutPath = Join-Path $PSScriptRoot assets WriteConsoleOut.ps1
$defaultEncoding = [Console]::OutputEncoding.WebName
}

BeforeEach {
Clear-Variable -Name PSApplicationOutputEncoding
}

AfterEach {
Clear-Variable -Name PSApplicationOutputEncoding
}

It 'Defaults to [Console]::OutputEncoding if not set' {
$actual = pwsh -File $WriteConsoleOutPath -Value café -Encoding $defaultEncoding
$actual | Should -Be café
}

It 'Defaults to [Console]::OutputEncoding if set to $null' {
$PSApplicationOutputEncoding = $null
$actual = pwsh -File $WriteConsoleOutPath -Value café -Encoding $defaultEncoding
$actual | Should -Be café
}

It 'Uses scoped $PSApplicationOutputEncoding value' {
$PSApplicationOutputEncoding = [System.Text.Encoding]::Unicode
$actual = & {
$PSApplicationOutputEncoding = [System.Text.UTF8Encoding]::new()
pwsh -File $WriteConsoleOutPath -Value café -Encoding utf-8
}

$actual | Should -Be café

# Will use UTF-16-LE hence the different values
$actual = pwsh -File $WriteConsoleOutPath -Value café -Encoding Unicode
$actual | Should -Be café
}

It 'Uses variable in class method' {
class NativeEncodingTestClass {
static [string] RunTest([string]$Script) {
$PSApplicationOutputEncoding = [System.Text.Encoding]::Unicode
return pwsh -File $Script -Value café -Encoding unicode
}
}

$actual = [NativeEncodingTestClass]::RunTest($WriteConsoleOutPath)
$actual | Should -Be café
}

It 'Fails to set variable with invalid encoding object' {
$ps = [PowerShell]::Create()
$ps.AddScript('$PSApplicationOutputEncoding = "utf-8"').Invoke()

$ps.Streams.Error.Count | Should -Be 1
[string]$ps.Streams.Error[0] | Should -Be 'Cannot convert the "utf-8" value of type "System.String" to type "System.Text.Encoding".'
}
}
23 changes: 23 additions & 0 deletions test/powershell/engine/Basic/assets/WriteConsoleOut.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.

[CmdletBinding()]
param (
[Parameter(Mandatory)]
[string]
$Value,

[Parameter(Mandatory)]
[string]
$Encoding
)

$enc = [System.Text.Encoding]::GetEncoding($Encoding)
$data = $enc.GetBytes($Value)

$outStream = [System.Console]::OpenStandardOutput()
try {
$outStream.Write($data, 0, $data.Length)
} finally {
$outStream.Dispose()
}
Loading