我的$ PSVersion输出如下:
Name Value
---- -----
PSVersion 7.0.1
PSEdition Core
GitCommitId 7.0.1
OS Microsoft Windows 10.0.19041
Platform Win32NT
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0
如果我跑步
wsl -l
我的输出在控制台中显示为:
Windows Subsystem for Linux Distributions:
Ubuntu-20.04 (Default)
ubuntu-20.04-systemd
如果我跑步
wsl -l | Out-File foo.txt
它在文件中显示为:
W i n d o w s S u b s y s t e m f o r L i n u x D i s t r i b u t i o n s :
U b u n t u - 2 0 . 0 4 ( D e f a u l t )
u b u n t u - 2 0 . 0 4 - s y s t e m d
我尝试为Out-File指定不同的输出编码无济于事。我想了解发生了什么以及如何解决它。
谢谢!
wsl -l
意外使用UTF-16LE(“统一”)字符编码输出,从而导致额外的NUL
字节(0x0
)得到插入ASCII码字符范围之间(可出现如空格)时,PowerShell的解释输出(它总是做,如果你捕获或重定向的输出;在控制台的显示是不受到影响),基于存储在所述编码[Console]::OutputEncoding
,缺省值为系统的活性OEM代码页。
解决方案是(暂时)设置[Console]::OutputEncoding
为matchwsl
的输出编码:
$orig = [Console]::OutputEncoding
[Console]::OutputEncoding = [System.Text.Encoding]::Unicode # UTF-16LE
wsl -l > foo.txt # > is effectively like Out-File
[Console]::OutputEncoding = $orig
有关自定义功能,请参见此答案Debug-String
,该功能有助于诊断此类编码问题。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句