Job cmdlets fail with UTF-8 codepage
Powershell jobs (eg, Start-Job, Wait-Job, Receive-Job) fail when Powershell's codepage is set to UTF8.
Simple reproduction on Server 2012R2 w/ PS5:
from cmd.exe, set the parent process codepage and start powershell with:
chcp 65001
powershell
from the powershell that just started:
$job = Start-Job -ScriptBlock { Write-Output yo }
Receive-Job $job -Wait
The Receive-Job call will fail with: "
[localhost] The background process reported an error with the following message: Cannot process an element with node type "Text". Only Element and EndElement node types are supported. at
System.Management.Automation.Remoting.OutOfProcessUtils.ProcesData(String data, DataProcessingDelegates callbacks).
+ CategoryInfo : OpenError: (localhost:String) [], PSRemotingTransportException
+ FullyQualifiedErrorId : 2100,PSSessionStateBroken
"
This is problematic for things like Ansible that want to use Powershell with arbitrary non-ASCII character sets (where setting region-specific codepages are kind of a non-starter).

4 comments
-
PetSerAl commented
@响叮咚 IMHO, it is a bug, but not PowerShell one. It is about how `ProcessStartInfo.RedirectStandardInput` is handled.
-
响叮咚 commented
that not bug, i think.
-
Matt Davis commented
Woot @PetSerAl, that works! I played around a bunch with the OutputEncoding, but didn't check for a BOM on the input side. Thanks!
-
PetSerAl commented
Use this as workaround:
if(
[Console]::InputEncoding -is [Text.UTF8Encoding] -and
[Console]::InputEncoding.GetPreamble().Length -ne 0
) {
[Console]::InputEncoding = New-Object Text.UTF8Encoding $false
}