-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #120 from szymonos/feat/wsl-get-distros
feat(ps): wsl_get_distros
- Loading branch information
Showing
1 changed file
with
98 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
#!/usr/bin/pwsh -nop | ||
<# | ||
.SYNOPSIS | ||
Script synopsis. | ||
.EXAMPLE | ||
wsl/wsl_get_distros.ps1 | ||
wsl/wsl_get_distros.ps1 -FromRegistry | ||
wsl/wsl_get_distros.ps1 -Online | ||
#> | ||
[CmdletBinding()] | ||
param ( | ||
[switch]$Online, | ||
|
||
[switch]$FromRegistry | ||
) | ||
|
||
begin { | ||
$ErrorActionPreference = 'Stop' | ||
|
||
if ($FromRegistry) { | ||
# specify list of properties to get from Windows registry lxss | ||
$prop = @( | ||
@{ Name = 'Name'; Expression = { $_.DistributionName } } | ||
'DefaultUid' | ||
@{ Name = 'Version'; Expression = { $_.Flags -lt 8 ? 1 : 2 } } | ||
) | ||
} else { | ||
$distros = [Collections.Generic.List[PSCustomObject]]::new() | ||
$outputEncoding = [Console]::OutputEncoding | ||
} | ||
} | ||
|
||
process { | ||
if ($FromRegistry) { | ||
# get list of WSL distros from Windows Registry | ||
$distros = Get-ChildItem HKCU:\Software\Microsoft\Windows\CurrentVersion\Lxss ` | ||
| ForEach-Object { $_ | Get-ItemProperty } ` | ||
| Where-Object { $_.DistributionName -notmatch '^docker-desktop' } ` | ||
| Select-Object $prop | ||
} else { | ||
# change console encoding to utf-16 | ||
[Console]::OutputEncoding = [System.Text.Encoding]::Unicode | ||
if ($Online) { | ||
# get list of online WSL distros | ||
[string[]]$result = wsl.exe --list --online | Where-Object { $_ } | ||
# get distros header | ||
[string]$head = $result | Select-String 'NAME\s+FRIENDLY' -CaseSensitive | Select-Object -ExpandProperty Line | ||
# calculate header line index | ||
$idx = $result.IndexOf($head) | ||
$dataIdx = if ($idx -ge 0) { | ||
$idx + 1 | ||
} else { | ||
$result.Count - 1 | ||
} | ||
# calculate header columns indexes | ||
$nameIdx = $head.IndexOf('NAME') | ||
$friendlyIdx = $head.IndexOf('FRIENDLY') | ||
# add results to the distros list | ||
for ($i = $dataIdx; $i -lt $result.Count; $i++) { | ||
$distro = [PSCustomObject]@{ | ||
Name = $result[$i].Substring($nameIdx, $friendlyIdx - $nameIdx).TrimEnd() | ||
FriendlyName = $result[$i].Substring($friendlyIdx, $result[$i].Length - $friendlyIdx).TrimEnd() | ||
} | ||
$distros.Add($distro) | ||
} | ||
} else { | ||
# get list of installed locally WSL distros | ||
[string[]]$result = wsl.exe --list --verbose | ||
# get distros header | ||
[string]$head = $result | Select-String 'NAME\s+STATE\s+VERSION' -CaseSensitive | Select-Object -ExpandProperty Line | ||
# calculate header line index | ||
$idx = $result.IndexOf($head) | ||
$dataIdx = if ($idx -ge 0) { | ||
$idx + 1 | ||
} else { | ||
$result.Count - 1 | ||
} | ||
# calculate header columns indexes | ||
$nameIdx = $head.IndexOf('NAME') | ||
$stateIdx = $head.IndexOf('STATE') | ||
$versionIdx = $head.IndexOf('VERSION') | ||
# add results to the distros list | ||
for ($i = $dataIdx; $i -lt $result.Count; $i++) { | ||
$distro = [PSCustomObject]@{ | ||
Name = $result[$i].Substring($nameIdx, $stateIdx - $nameIdx).TrimEnd() | ||
State = $result[$i].Substring($stateIdx, $versionIdx - $stateIdx).TrimEnd() | ||
Version = $result[$i].Substring($versionIdx, $result[$i].Length - $versionIdx).TrimEnd() | ||
} | ||
$distros.Add($distro) | ||
} | ||
} | ||
[Console]::OutputEncoding = $outputEncoding | ||
} | ||
} | ||
|
||
end { | ||
return $distros | ||
} |