diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Tee-Object.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Tee-Object.cs index 32048362b06..e78f991161b 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Tee-Object.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Tee-Object.cs @@ -3,6 +3,7 @@ using System; using System.Management.Automation; +using System.Text; using Microsoft.PowerShell.Commands.Internal.Format; @@ -72,6 +73,16 @@ public SwitchParameter Append private bool _append; + /// + /// Gets or sets the Encoding. + /// + [Parameter(ParameterSetName = "File")] + [Parameter(ParameterSetName = "LiteralFile")] + [ArgumentToEncodingTransformationAttribute] + [ArgumentEncodingCompletionsAttribute] + [ValidateNotNullOrEmpty] + public Encoding Encoding { get; set; } = ClrFacade.GetDefaultEncoding(); + /// /// Variable parameter. /// @@ -95,12 +106,14 @@ protected override void BeginProcessing() _commandWrapper.Initialize(Context, "out-file", typeof(OutFileCommand)); _commandWrapper.AddNamedParameter("filepath", _fileName); _commandWrapper.AddNamedParameter("append", _append); + _commandWrapper.AddNamedParameter("encoding", Encoding); } else if (string.Equals(ParameterSetName, "LiteralFile", StringComparison.OrdinalIgnoreCase)) { _commandWrapper.Initialize(Context, "out-file", typeof(OutFileCommand)); _commandWrapper.AddNamedParameter("LiteralPath", _fileName); _commandWrapper.AddNamedParameter("append", _append); + _commandWrapper.AddNamedParameter("encoding", Encoding); } else { diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Tee-Object.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Tee-Object.Tests.ps1 index 01527be8435..7483c4d8004 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Tee-Object.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Tee-Object.Tests.ps1 @@ -4,21 +4,39 @@ Describe "Tee-Object" -Tags "CI" { Context "Validate Tee-Object is correctly forking output" { - $testfile = Join-Path -Path (Join-Path -Path $PSScriptRoot -ChildPath assets) -ChildPath testfile.txt + BeforeAll { + $testfile = Join-Path $TestDrive -ChildPath "testfile.txt" + $testvalue = "ф" + if ($IsWindows) { + # Expected bytes: 244 - 'ф', 13 - '`r', 10 - '`n'. + $expectedBytes = 244,13,10 -join "-" + } else { + $expectedBytes = 244,10 -join "-" + } + } + + BeforeEach { + Remove-Item -Path $testfile -ErrorAction SilentlyContinue -Force + } It "Should return the output to the screen and to the variable" { - $teefile = $testfile Write-Output teeobjecttest1 | Tee-Object -Variable teeresults - $teeresults | Should -BeExactly "teeobjecttest1" - Remove-Item $teefile -ErrorAction SilentlyContinue + $teeresults | Should -BeExactly "teeobjecttest1" } It "Should tee the output to a file" { $teefile = $testfile Write-Output teeobjecttest3 | Tee-Object $teefile Get-Content $teefile | Should -BeExactly "teeobjecttest3" - Remove-Item $teefile -ErrorAction SilentlyContinue - } + } + + It "Parameter 'Encoding' should accept encoding" { + $teefile = $testfile + $encoding = 1251 + $testvalue | Tee-Object -Encoding $encoding $teefile + Get-Content $teefile -Encoding $encoding | Should -BeExactly $testvalue + (Get-Content $teefile -AsByteStream) -join "-" | Should -BeExactly $expectedBytes + } } }