Skip to content

Commit

Permalink
Merge pull request #120 from szymonos/feat/wsl-get-distros
Browse files Browse the repository at this point in the history
feat(ps): wsl_get_distros
  • Loading branch information
szymonos authored Sep 29, 2023
2 parents 2710785 + c978a5a commit 9382aff
Showing 1 changed file with 98 additions and 0 deletions.
98 changes: 98 additions & 0 deletions wsl/wsl_get_distros.ps1
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
}

0 comments on commit 9382aff

Please sign in to comment.