Wsl2DebianEnv/scripts/system/check-wsl.ps1
Mars 6ddba3e722 Initial commit
feat: 完成 Zsh 插件配置脚本的优化和测试 - 修复 autojump 配置冲突 - 清理重复的插件安装 - 优化脚本结构和注释

feat: 优化shell脚本 - 1. 增强错误处理和恢复机制 2. 添加网络连接优化和镜像源支持 3. 改进进度显示和用户交互 4. 优化配置文件管理和备份 5. 改进插件管理机制 6. 增强依赖检查和安装 7. 添加完整的日志记录功能 8. 修复字体安装相关问题

docs: 完善shell、system和utils部分的README文档 - 1. 添加详细的脚本说明和使用方法 2. 补充依赖要求和注意事项 3. 添加常见问题解答 4. 更新版本日志

feat(editor): 优化 neovim 和 nvchad 安装脚本,添加性能优化配置和详细文档

feat: 优化Python开发环境安装脚本,分离基础包和机器学习包,修复virtualenvwrapper配置

feat: 优化开发工具安装脚本,统一使用common.sh中的函数,改进错误处理

fix: 修复DNMP安装脚本,跳过自动配置PHP开发环境

fix: 提交删除的 init.sh 和 mysql.sh 文件
2025-03-25 16:14:03 +08:00

188 lines
5.5 KiB
PowerShell

# WSL2 Environment Check Script
# This script checks WSL2 installation status on Windows
# Set error action
$ErrorActionPreference = 'Stop'
# Color definitions
$Red = [System.ConsoleColor]::Red
$Green = [System.ConsoleColor]::Green
$Yellow = [System.ConsoleColor]::Yellow
# Logging functions
function Write-Info {
param([string]$Message)
Write-Host '[INFO] ' -NoNewline -ForegroundColor $Green
Write-Host $Message
}
function Write-Warn {
param([string]$Message)
Write-Host '[WARN] ' -NoNewline -ForegroundColor $Yellow
Write-Host $Message
}
function Write-Error {
param([string]$Message)
Write-Host '[ERROR] ' -NoNewline -ForegroundColor $Red
Write-Host $Message
}
function Wait-KeyPress {
Write-Host "`nPress any key to exit..." -NoNewline
$null = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown')
Write-Host ''
}
# Check administrator privileges
if (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
Write-Warn 'Administrator privileges required'
Write-Info 'Please right-click this script and select "Run as Administrator"'
Wait-KeyPress
exit 1
}
Write-Info "`nStarting WSL2 environment check...`n"
# Check Windows version
Write-Info 'Checking Windows version...'
$windowsVersion = (Get-WmiObject Win32_OperatingSystem).Caption
Write-Host "Windows Version: $windowsVersion"
# Check WSL installation status
Write-Info 'Checking WSL installation...'
try {
$wslVersion = wsl --version
Write-Info 'WSL is installed'
Write-Host "`n$wslVersion`n"
} catch {
Write-Error 'WSL is not installed'
Write-Info 'Please visit https://aka.ms/wsl to install WSL'
Wait-KeyPress
exit 1
} finally {
# Clear errors
$error.clear()
}
# Check WSL2 status
Write-Info 'Checking WSL2 status...'
try {
$wslList = wsl -l -v
if ($wslList -match '2') {
Write-Info 'WSL2 is enabled'
} else {
Write-Warn 'WSL2 is not enabled'
Write-Info 'WSL distribution list:'
Write-Host "`n$wslList`n"
Write-Info 'Please run the following command to enable WSL2:'
Write-Host 'wsl --set-default-version 2'
Wait-KeyPress
exit 1
}
} catch {
Write-Error "Failed to get WSL status: $($_.Exception.Message)"
Wait-KeyPress
exit 1
} finally {
# Clear errors
$error.clear()
}
# Check Debian installation status
Write-Info 'Checking Debian installation...'
try {
wsl -d Debian echo 'Debian is installed' | Out-Null
Write-Info 'Debian is installed'
} catch {
Write-Warn 'Debian is not installed'
Write-Info 'Please run the following command to install Debian:'
Write-Host 'wsl --install -d Debian'
Wait-KeyPress
exit 1
} finally {
# Clear errors
$error.clear()
}
# Check virtualization status
Write-Info 'Checking virtualization status...'
try {
$systemInfo = systeminfo
if ($systemInfo -match 'Virtual Ethernet Adapter' -or $systemInfo -match 'hypervisor has been detected') {
Write-Info 'Virtualization is enabled'
} else {
Write-Error 'Virtualization is not enabled'
Write-Info 'Please enable virtualization (Intel VT-x or AMD-V) in BIOS'
Wait-KeyPress
exit 1
}
} catch {
Write-Error "Failed to check virtualization status: $($_.Exception.Message)"
Wait-KeyPress
exit 1
} finally {
# Clear errors
$error.clear()
}
# Check Windows features
Write-Info 'Checking Windows features...'
# Check Linux subsystem
try {
$wslFeature = Get-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux
if ($wslFeature.State -eq 'Enabled') {
Write-Info 'Linux subsystem is enabled'
} else {
Write-Warn 'Linux subsystem is not enabled'
Write-Info 'Please run the following command to enable Linux subsystem:'
Write-Host 'dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart'
Wait-KeyPress
exit 1
}
} catch {
Write-Error "Failed to check Linux subsystem status: $($_.Exception.Message)"
Wait-KeyPress
exit 1
} finally {
# Clear errors
$error.clear()
}
# Check virtual machine platform
try {
$vmPlatform = Get-WindowsOptionalFeature -Online -FeatureName VirtualMachinePlatform
if ($vmPlatform.State -eq 'Enabled') {
Write-Info 'Virtual Machine Platform is enabled'
} else {
Write-Warn 'Virtual Machine Platform is not enabled'
Write-Info 'Please run the following command to enable Virtual Machine Platform:'
Write-Host 'dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart'
Wait-KeyPress
exit 1
}
} catch {
Write-Error "Failed to check Virtual Machine Platform status: $($_.Exception.Message)"
Wait-KeyPress
exit 1
} finally {
# Clear errors
$error.clear()
}
Write-Host "`n============================================"
Write-Host " Check Complete! "
Write-Host "============================================"
Write-Host "`nYour WSL2 environment is ready:`n"
Write-Info '1. Windows version is compatible'
Write-Info '2. WSL2 is installed and enabled'
Write-Info '3. Debian distribution is installed'
Write-Info '4. Virtualization is enabled'
Write-Info '5. Required Windows features are enabled'
Write-Info 'You can now start using WSL2!'
Write-Host "============================================"
# Wait for user input before exit
Wait-KeyPress