I don't think the question needs a lot of clarification, especially when you see what I tried:
wsl -l -q --running | Where{$_ -ne ""} | ForEach {Write-Output "Distro $_"; wsl -d $_ -e ls; Write-Output "/// $_"}
I wanted/expected the above to write "Distro (distribution name)
" on the console, then execute ls
on that distribution, and then write "/// (distribution name)
" on the console at the end, for each active VM. The part that doesn't work is the ls
– instead of executing ls
, I get the generic help message from wsl
.
I think I'm not escaping something properly, because if I try to deliberately mangle the command by removing the distro name parameter (i.e. wsl -d -e ls
instead of wsl -d $_ -e ls
) I get a sensible error message (There is no distribution with the supplied name.
) instead of the generic help message.
On the other hand, if I just run wsl -d Ubuntu-20.04 -e ls
manually in a console, that behaves as expected.
What am I doing wrong?
CodePudding user response:
It turns out my problem was caused by wsl
. Amazingly, it spits out the distro names in a bizarre encoding, which it then refuses when that gets fed back to it. Check it out:
wsl -l | Format-Hex
My solution (at least for the time being) is to just remove null characters from wsl
's output:
(wsl -l) -replace "`0", "" | Format-Hex
looks much better.
We just have to use this trick in our original script:
(wsl -l -q --running) -replace "`0", "" | Where{$_ -ne ""} | ForEach {Write-Output "Distro $_"; wsl -d $_ -e ls; Write-Output "/// $_"}