Wsl2DebianEnv/scripts/system/check_windows_wsl.ps1

192 lines
5.1 KiB
PowerShell
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# WSL2 环境检查脚本
# 此脚本用于检查 Windows 系统上的 WSL2 安装状态
# 设置错误操作
$ErrorActionPreference = "Stop"
# 颜色定义
$Red = [System.ConsoleColor]::Red
$Green = [System.ConsoleColor]::Green
$Yellow = [System.ConsoleColor]::Yellow
# 日志函数
function Write-Info {
param([string]$Message)
Write-Host "[INFO] $Message" -ForegroundColor $Green
}
function Write-Warn {
param([string]$Message)
Write-Host "[WARN] $Message" -ForegroundColor $Yellow
}
function Write-Error {
param([string]$Message)
Write-Host "[ERROR] $Message" -ForegroundColor $Red
}
# 检查 Windows 版本
function Test-WindowsVersion {
Write-Info "检查 Windows 版本..."
$os = Get-WmiObject -Class Win32_OperatingSystem
$version = [System.Environment]::OSVersion.Version
Write-Info "Windows 版本: $($os.Caption)"
Write-Info "系统版本号: $($version.Major).$($version.Minor).$($version.Build)"
if ($version.Major -lt 10 -or ($version.Major -eq 10 -and $version.Build -lt 19041)) {
Write-Error "Windows 版本不满足 WSL2 要求,需要 Windows 10 版本 2004 或更高版本"
return $false
}
return $true
}
# 检查 WSL 是否安装
function Test-WSLInstallation {
Write-Info "检查 WSL 安装状态..."
try {
$wslVersion = wsl --version 2>$null
if ($LASTEXITCODE -eq 0) {
Write-Info "WSL 已安装"
Write-Info "WSL 版本信息:"
Write-Host $wslVersion
return $true
}
}
catch {
Write-Error "WSL 未安装"
Write-Info "请访问 https://aka.ms/wsl 安装 WSL"
return $false
}
}
# 检查 WSL2 是否启用
function Test-WSL2Enabled {
Write-Info "检查 WSL2 状态..."
try {
$wslList = wsl -l -v
if ($wslList -match "VERSION.*2") {
Write-Info "WSL2 已启用"
Write-Info "WSL 发行版列表:"
Write-Host $wslList
return $true
}
else {
Write-Warn "WSL2 未启用"
Write-Info "请运行以下命令启用 WSL2"
Write-Host "wsl --set-default-version 2"
return $false
}
}
catch {
Write-Error "获取 WSL 版本信息失败"
return $false
}
}
# 检查虚拟化是否启用
function Test-VirtualizationEnabled {
Write-Info "检查虚拟化状态..."
try {
$vmx = Get-WmiObject -Class Win32_Processor | Select-Object -ExpandProperty NumberOfLogicalProcessors
if ($vmx -gt 0) {
Write-Info "虚拟化已启用"
return $true
}
else {
Write-Error "虚拟化未启用"
Write-Info "请在 BIOS 中启用虚拟化技术Intel VT-x 或 AMD-V"
return $false
}
}
catch {
Write-Error "检查虚拟化状态失败"
return $false
}
}
# 检查 Debian 是否安装
function Test-DebianInstallation {
Write-Info "检查 Debian 安装状态..."
try {
$wslList = wsl -l
if ($wslList -match "Debian") {
Write-Info "Debian 已安装"
return $true
}
else {
Write-Warn "Debian 未安装"
Write-Info "请运行以下命令安装 Debian"
Write-Host "wsl --install -d Debian"
return $false
}
}
catch {
Write-Error "检查 Debian 安装状态失败"
return $false
}
}
# 检查 Windows 功能
function Test-WindowsFeatures {
Write-Info "检查 Windows 功能..."
$features = @(
"Microsoft-Windows-Subsystem-Linux",
"VirtualMachinePlatform"
)
$allEnabled = $true
foreach ($feature in $features) {
$status = Get-WindowsOptionalFeature -Online -FeatureName $feature
if ($status.State -eq "Enabled") {
Write-Info "$feature 已启用"
}
else {
Write-Warn "$feature 未启用"
Write-Info "请运行以下命令启用 $feature"
Write-Host "dism.exe /online /enable-feature /featurename:$feature /all /norestart"
$allEnabled = $false
}
}
return $allEnabled
}
# 主函数
function Main {
Write-Info "开始检查 WSL2 环境..."
$checks = @{
"Windows 版本检查" = { Test-WindowsVersion }
"WSL 安装检查" = { Test-WSLInstallation }
"WSL2 状态检查" = { Test-WSL2Enabled }
"虚拟化检查" = { Test-VirtualizationEnabled }
"Debian 安装检查" = { Test-DebianInstallation }
"Windows 功能检查" = { Test-WindowsFeatures }
}
$allPassed = $true
foreach ($check in $checks.GetEnumerator()) {
Write-Host "`n=== $($check.Key) ==="
if (-not (& $check.Value)) {
$allPassed = $false
}
}
if ($allPassed) {
Write-Info "`n所有检查项通过WSL2 环境已准备就绪。"
}
else {
Write-Warn "`n部分检查项未通过,请按照提示进行修复。"
}
}
# 执行主函数
Main