简体   繁体   English

PowerShell 中 Win32 可执行输出的字母之间的单个空格

[英]Single space between letters of Win32 executable output in PowerShell

my $PSVersion output is as follows:我的 $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

If I run如果我跑

wsl -l

my output appears in the console as:我的输出在控制台中显示为:

Windows Subsystem for Linux Distributions:
Ubuntu-20.04 (Default)
ubuntu-20.04-systemd

If I run如果我跑

wsl -l | Out-File foo.txt

it appears in the file as:它在文件中显示为:

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 
 

I've tried specifying different output encodings to Out-File to no avail.我试过为 Out-File 指定不同的输出编码无济于事。 I'd like to understand what's going on and how to get around it.我想了解发生了什么以及如何解决它。

Thank you!谢谢!

wsl -l unexpectedly uses the UTF-16LE ("Unicode") character encoding for its output , resulting in extra NUL bytes ( 0x0 ) getting inserted between ASCII-range characters (which can appear like spaces) when PowerShell interprets the output (which it invariably does if you capture or redirect the output; display in the console is not affected), based on the encoding stored in [Console]::OutputEncoding , which defaults to the system's active OEM code page. wsl -l意外使用UTF-16LE(“统一”)字符编码它的输出,从而导致额外的NUL字节( 0x0得到插入ASCII范围字符之间(其可以出现像空格)时的PowerShell解释输出)(其如果捕获或重定向的输出总是确实;在控制台显示不受影响),基于存储在所述编码[Console]::OutputEncoding ,缺省值为系统的活性OEM代码页。

The solution is to (temporarily) set [Console]::OutputEncoding to match wsl 's output encoding :解决方案是(暂时)设置[Console]::OutputEncoding以匹配wsl的输出编码

$orig = [Console]::OutputEncoding
[Console]::OutputEncoding = [System.Text.Encoding]::Unicode # UTF-16LE

wsl -l > foo.txt # > is effectively like Out-File

[Console]::OutputEncoding = $orig

See this answer for custom function Debug-String , which facilitates diagnosing such encoding problems.请参阅自定义函数Debug-String此答案,它有助于诊断此类编码问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM