function Get-WslStatus { param ( [string]$Name = "*" #既定のLinuxディストリビューション(ワイルドカードではない) ) $Status = "unknown" $offset = 0 $default = $false $lines = wsl.exe -l -v | Out-String -Stream #$lines = $lines | ForEach-Object { $_ -replace '\p{Cc}+', '' } $lines = $lines | ForEach-Object { $_ -replace '\x00', '' } foreach ($line in $lines) { if ([string]::IsNullOrWhiteSpace($line)) { continue } if ($line -match '^\s*NAME\s+STATE\s+VERSION\s*$') { continue } if ($line.ToLower().Contains($Name.ToLower())) { $line = $line -split "\s+" |Where {$_ -ne ""} if ($line.Count -eq 3) { $offset = -1 } elseif (($line.Count -eq 4) -and $line.Contains("*")) { $default = $true } return [pscustomobject]@{ Name = $line[1+$offset] Status = $line[2+$offset] Version = $line[3+$offset] IsDefault = $default } } } Write-Error "$($Name) は見つからないか、状態が不明です。" } # Usage: # Get-WslStatus "Linuxディストリビューション名(省略または * は既定のディストリビューション)" Get-WslStatus