-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGet_WSL_info.ps1
More file actions
177 lines (149 loc) · 7.24 KB
/
Get_WSL_info.ps1
File metadata and controls
177 lines (149 loc) · 7.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# Get_WSL_info.ps1
# ---------------------------------------------
# This script gathers information about the Windows Subsystem for Linux (WSL) installation,
# including the status of WSL features and installed distributions.
# ---------------------------------------------
function Get-WSLInfo {
[CmdletBinding()]
param()
Write-Host "Gathering WSL Information..." -ForegroundColor Cyan
Write-Host "--------------------------------" -ForegroundColor Gray
# Check if wsl is available in path
$wslCommand = Get-Command wsl.exe -ErrorAction SilentlyContinue
if (-not $wslCommand) {
Write-Warning "WSL Executable (wsl.exe) not found in system PATH."
Write-Host "Please ensure Windows Subsystem for Linux is installed."
return
}
Write-Host "WSL Executable found at: $($wslCommand.Source)" -ForegroundColor Green
# Check BIOS Virtualization
try {
$cpuInfo = Get-CimInstance -ClassName Win32_Processor -ErrorAction Stop | Select-Object -First 1
if ($cpuInfo.PSObject.Properties['VirtualizationFirmwareEnabled']) {
$virtEnabled = $cpuInfo.VirtualizationFirmwareEnabled
$virtColor = if ($virtEnabled) { 'Green' } else { 'Red' }
Write-Host "BIOS Virtualization Enabled: $virtEnabled" -ForegroundColor $virtColor
} else {
Write-Host "BIOS Virtualization Status: Unknown (Property not available)" -ForegroundColor Gray
}
} catch {
Write-Warning "Could not determine BIOS Virtualization status. $_"
}
# Check Windows Optional Feature (Requires Admin)
$currentUser = [Security.Principal.WindowsIdentity]::GetCurrent()
$principal = [Security.Principal.WindowsPrincipal]$currentUser
if ($principal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
try {
$wslFeature = Get-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux -ErrorAction Stop
$wslColor = if ($wslFeature.State -eq 'Enabled') { 'Green' } else { 'Yellow' }
Write-Host "WSL Optional Feature State: $($wslFeature.State)" -ForegroundColor $wslColor
$vmpFeature = Get-WindowsOptionalFeature -Online -FeatureName VirtualMachinePlatform -ErrorAction SilentlyContinue
if ($vmpFeature) {
$vmpColor = if ($vmpFeature.State -eq 'Enabled') { 'Green' } else { 'Yellow' }
Write-Host "Virtual Machine Platform State: $($vmpFeature.State)" -ForegroundColor $vmpColor
}
}
catch {
Write-Warning "Could not query Windows Optional Features. $_"
}
} else {
Write-Host "Skipping specific feature check (requires Administrator privileges)." -ForegroundColor Gray
}
# Check WSL Status (wsl --status)
Write-Host "`n--- WSL Status ---" -ForegroundColor White
$wslStatusSuccess = $false
try {
$statusOutput = wsl.exe --status 2>&1
if ($LASTEXITCODE -eq 0) {
$statusOutput | ForEach-Object { Write-Host $_ -ForegroundColor Gray }
$wslStatusSuccess = $true
} else {
Write-Host "Could not retrieve detailed status (wsl --status returned no info or failed)." -ForegroundColor Yellow
Write-Host "Output: $($statusOutput | Out-String)" -ForegroundColor DarkGray
}
} catch {
Write-Warning "Failed to run 'wsl --status'."
}
if (-not $wslStatusSuccess) {
Write-Warning "WSL does not appear to be correctly installed or running. Skipping distribution list."
return
}
# Check Installed Distributions (wsl --list --verbose)
Write-Host "`n--- Installed Distributions ---" -ForegroundColor White
try {
# wsl.exe output encoding is annoying
$listOutput = wsl.exe --list --verbose
# Filter out empty lines
$lines = $listOutput | Where-Object { -not [string]::IsNullOrWhiteSpace($_) }
# Check for valid list header to avoid parsing help text or error messages
$headerFound = $false
foreach ($line in $lines) {
# Remove null bytes
$cleanLine = $line -replace '\x00', ''
# Check if line looks like a header using wildcards
if ($cleanLine -like "*NAME*STATE*") {
$headerFound = $true
break
}
}
if (-not $headerFound) {
Write-Host "It appears no distributions are installed or the output format is unexpected." -ForegroundColor Yellow
Write-Host "Raw Output:" -ForegroundColor DarkGray
$lines | ForEach-Object { Write-Host $_ -ForegroundColor DarkGray }
# Helper message if it looks like help text
if ($lines -join "`n" -like "*--install*") {
Write-Host "`nUse 'wsl.exe --install' to install default distribution." -ForegroundColor Cyan
}
return
}
if ($lines.Count -le 1) {
# Try listing if verbose fails or returns bad
if ($lines.Count -eq 0) {
Write-Host "No output for distribution list." -ForegroundColor Yellow
} else {
# Maybe header only?
Write-Host "No distributions appear to be installed." -ForegroundColor Yellow
}
}
else {
$distroObjects = @()
# Skip the header row (row 0)
for ($i = 1; $i -lt $lines.Count; $i++) {
$line = $lines[$i]
# Check for default marker '*'
$isDefault = $false
if ($line.TrimStart().StartsWith("*")) {
$isDefault = $true
$line = $line.Replace("*", " ") # Replace first * with space
}
# Remove control chars (including nulls) and replace weird whitespace with space
$line = $line -replace '\x00', '' -replace '[\p{C}]', '' -replace '[\p{Z}]', ' '
$line = $line.Trim()
if ([string]::IsNullOrWhiteSpace($line)) { continue }
# Split by whitespace and filter empty entries
$parts = $line -split ' +' | Where-Object { -not [string]::IsNullOrWhiteSpace($_) }
# Expected output: Name, State, Version
if ($parts.Count -ge 3) {
$distroObjects += [PSCustomObject]@{
'IsDefault' = $isDefault
'Name' = $parts[0]
'State' = $parts[1]
'Version' = $parts[2]
}
}
}
if ($distroObjects.Count -gt 0) {
# Display properties list
$distroObjects | Format-Table -AutoSize
} else {
Write-Host "No valid distributions found in output." -ForegroundColor Yellow
Write-Host "Raw Output associated with parsing failure:" -ForegroundColor DarkGray
$lines | ForEach-Object { Write-Host $_ -ForegroundColor DarkGray }
}
}
} catch {
Write-Warning "Failed to retrieve distribution list. $_"
}
}
Get-WSLInfo
pause