microsoft / WSL

Issues found on WSL
https://docs.microsoft.com/windows/wsl
MIT License
17.15k stars 802 forks source link

Cannot read WSL2 distro-state from powershell #9241

Open MBauerDC opened 1 year ago

MBauerDC commented 1 year ago

Version

Microsoft Windows [Version 10.0.22621.819]

WSL Version

Kernel Version

5.15.74.2-microsoft-standard-WSL2

Distro Version

any

Other Software

Powershell - reproduced on two versions:

PSVersion 7.3.0 PSEdition Core GitCommitId 7.3.0 OS Microsoft Windows 10.0.22621 Platform Win32NT PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…} PSRemotingProtocolVersion 2.3 SerializationVersion 1.1.0.1 WSManStackVersion 3.0

PSVersion 5.1.22621.608 PSEdition Desktop PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...} BuildVersion 10.0.22621.608 CLRVersion 4.0.30319.42000 WSManStackVersion 3.0 PSRemotingProtocolVersion 2.3 SerializationVersion 1.1.0.1

Repro Steps

  1. Install multiple distros
  2. Ensure wsl -l -v shows all installed distros as having state "Running"
  3. Terminate a distro via wsl -t <distro-name>
  4. Ensure wsl -l -v shows the terminated distro as having state "Stopped"
  5. In Powershell, iterate over Distro-Information via Get-ChildItem "HKCU:\Software\Microsoft\Windows\CurrentVersion\Lxss" -Recurse and report the object-properties, e.g. via piping to ForEach-Object { Write-Host ($_ | Format-Table | Out-String) }

Expected Behavior

The reported state-property of stopped distros is different from the reported State-Property of running distros.

Actual Behavior

The state-property of all installed distros is reported as "1"

(Note: If there is better method to get the state-property of distros from powershell, I will gladly use it - but this was all I could find online. Programmatic access from Powershell to WSL distro properties does not seem well documented.)

Diagnostic Logs

No response

creizlein commented 1 year ago

I am facing a similar situation where I need to programmatically get the Distro State but as you mention the registry value is not being updated properly and only way I had found is by running a new process with wsl -l -v. There is no even a p/invoke or API to get distro state.