I want to see if I'm running a particular wsl distribution (Windows 10 Home, WSL 2):
我想知道我是否正在运行特定的WSL发行版(Windows 10 Home,WSL 2):
PS C:\Users\User> wsl --list --running
Windows Subsystem for Linux Distributions:
Ubuntu (Default)
MyDistro
PS C:\Users\User> wsl --list --running | Select-String -Pattern "MyDistro"
PS C:\Users\User>
No output. I used Get-Member
to see that the output is a string; if I run it through something like | Out-String -stream
it makes no difference.
无输出。我使用Get-Members来查看输出是一个字符串;如果我通过类似|out-string-stream这样的内容运行它,也没有什么不同。
I can get a match with Select-String .
or Select-String .*
but it matches everything, which isn't helpful.
我可以用选择字符串进行匹配。或者Select-String.*,但它匹配所有内容,这是没有帮助的。
Yes, I want to see if there's a running distro with a particular name. Is there a better way to do that in PowerShell?
是的,我想看看是否有一个特定名称的运行发行版。在PowerShell中有没有更好的方法来做到这一点?
更多回答
Inexplicably, wsl --list --running
produces UTF-16LE-encoded ("Unicode"-encoded) output by default instead of respecting the console's active code page, which defaults to the legacy system locale's OEM code page.
令人费解的是,运行wsl--list--默认情况下会生成UTF-16LE编码(“Unicode”编码)输出,而不是遵循控制台的活动代码页,后者默认为传统系统区域设置的OEM代码页。
Update: As noted in Vimes' answer, v0.64 and above of wsl.exe
(verify via the first line output by wsl.exe --version
) now support setting the WSL_UTF8
environment variable to 1
to make wsl.exe
output UTF-8-encoded output:
更新:正如Vimes的回答中指出的,wsl.exe的v0.64及更高版本(通过wsl.exe--Version的第一行输出进行验证)现在支持将WSL_UTF8环境变量设置为1,以使wsl.exe输出UTF-8编码的输出:
$env:WSL_UTF8 = 1
wsl --list --running | Select-String -Pattern MyDistro
This works, as long as your distro names comprise ASCII-range characters only (which seems likely), given that UTF-8 is a superset of ASCII, just like the OEM code pages are.
If you happen to have configured your system to use UTF-8 system-wide, (in which case both the OEM and the ANSI code pages are UTF-8 - see this answer), the above solution works even with non-ASCII-range characters.
只要您的发行版名称只包含ASCII范围的字符(这似乎是可能的),这就是可行的,因为UTF-8是ASCII的超集,就像OEM代码页一样。如果您恰好将系统配置为在系统范围内使用UTF-8(在这种情况下,OEM和ANSI代码页都是UTF-8--请参阅此答案),则上述解决方案甚至适用于非ASCII范围的字符。
Otherwise, in the (unlikely) event that your distros contain non-ASCII-range characters (e.g. accented characters such as é
), use the second workaround below.
否则,如果(不太可能)您的发行版包含非ASCII范围的字符(例如,é等重音字符),请使用下面的第二个解决方法。
Without use of $env:WSL_UTF8
:
不使用$env:wsl_utf8:
A quick-and-dirty workaround - which assumes that all running distros are described only with ASCII-range characters (which seems likely) - is to use the following:
一种快速而肮脏的解决方法-假设所有运行的发行版仅使用ASCII范围字符描述(看起来很可能)-使用以下内容:
(wsl --list --running) -replace "`0" | Select-String -Pattern MyDistro
A proper workaround that supports all Unicode characters requires more effort:
支持所有Unicode字符的正确解决方法需要付出更多努力:
$prev = [Console]::OutputEncoding; [Console]::OutputEncoding = [System.Text.Encoding]::Unicode
wsl --list --running | Select-String -Pattern MyDistro
[Console]::OutputEncoding = $prev
I just found that $env:WSL_UTF8=1
makes wsl.exe output UTF8. Select-String and More seem to work now!
我刚刚发现$env:WSL_UTF8=1使wsl.exe输出UTF8。SELECT-STRING和更多似乎现在起作用了!
(Prevoius answer: If you don't need regex then Select-String -simplematch
seems to work.)
(Prevoius回答:如果你不需要正则表达式,那么Select-String -simplematch似乎可以工作。
更多回答
Wow, how did you figure that out?
你怎么知道的
@Vimes, I analyzed the wsl.exe
output character by character, using the technique shown in this answer.
@Vimes,我使用了这个答案中所示的技术,逐个字符地分析了wsl.exe输出。
+1 for the $env:WSL_UTF8
hint (I took the liberty of updating my answer based on it). As for the previous answer: note that regex vs. literal string search is a red herring; the only cause of the problem is the misinterpretation of the UTF-16LE output by PowerShell, before any subsequent command sees the resulting .NET string.
$env:WSL_UTF8提示+1(我擅自根据它更新了我的答案)。至于前面的答案:请注意,正则表达式和文字字符串搜索是在转移注意力;问题的唯一原因是在任何后续命令看到结果.NET字符串之前,PowerShell对UTF-16LE输出的误解。
我是一名优秀的程序员,十分优秀!