Windows 快速检测 Docker / WSL2 安装环境脚本(附 GUI 版本)

标签:Windows、Docker、WSL2、PowerShell、自动化运维
适用人群:想要快速判断客户电脑能不能装 Docker Desktop / WSL2 的工程师、运维、售前


1️⃣ 为什么要写这个脚本?

很多时候,我们需要给客户部署 Docker Desktop 或基于 WSL2 的开发环境,
结果发现:

  • 系统架构是 32 位 ❌

  • CPU 不支持虚拟化 ❌

  • Windows 版本太老 ❌

  • 内存 / 磁盘不够 ❌

导致前期准备浪费时间,客户体验也不好。

所以我写了一个 一键检测脚本,直接告诉你客户的电脑是否适合安装,省时省力。


2️⃣ 脚本检测逻辑

脚本会自动检测以下项目:

  • ✅ 系统架构:必须是 64 位

  • ✅ CPU 虚拟化:必须开启

  • ✅ Windows 版本:至少 1903 / 内部版本 18362

  • ✅ 内存:≥ 4 GB

  • ✅ 磁盘空间:≥ 20 GB

  • ✅ Hyper-V 和 WSL 功能是否启用

最后给出 清晰的结论

  • ✅ 可以安装

  • ❌ 不可以安装


3️⃣ 一键检测脚本(命令行版)

将以下代码保存为 DockerWSL_Check_Client.ps1

Clear-Host
$errorFlag = $false

# 系统架构
$arch = (Get-ComputerInfo).OsArchitecture
if ($arch -notlike "*64*") { $errorFlag = $true }

# CPU 虚拟化
$vmSupport = (Get-ComputerInfo).HyperVRequirementVirtualizationFirmwareEnabled
if (-not $vmSupport) { $errorFlag = $true }

# Windows 版本
$winBuild = [int](Get-ComputerInfo).OsBuildNumber
if ($winBuild -lt 18362) { $errorFlag = $true }

# 内存检查
$memGB = ((Get-ComputerInfo).CsTotalPhysicalMemory / 1GB)
if ($memGB -lt 4) { $errorFlag = $true }

# 磁盘空间检查
$diskFreeGB = ((Get-PSDrive C).Free / 1GB)
if ($diskFreeGB -lt 20) { $errorFlag = $true }

# Hyper-V / WSL 功能检查
$hyperV = (Get-WindowsOptionalFeature -Online | Where-Object {$_.FeatureName -like "Microsoft-Hyper-V*"}).State
$wsl = (Get-WindowsOptionalFeature -Online | Where-Object {$_.FeatureName -like "*Subsystem-Linux*"}).State
if ($hyperV -ne "Enabled" -or $wsl -ne "Enabled") { $errorFlag = $true }

Write-Host "`n===== 检测结果 ====="
if ($errorFlag) {
    Write-Host "❌ 系统不适合安装 Docker Desktop / WSL2" -ForegroundColor Red
} else {
    Write-Host "✅ 系统适合安装 Docker Desktop / WSL2" -ForegroundColor Green
}

4️⃣ 使用方法

  1. 保存脚本到桌面,例如 DockerWSL_Check_Client.ps1

  2. 右键 以管理员身份运行 PowerShell

  3. 运行命令:

Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass
.\DockerWSL_Check_Client.ps1

屏幕会显示 ✅ 或 ❌。


5️⃣ GUI 图形化版本

如果客户不熟悉 PowerShell,可以用图形化版本:

Add-Type -AssemblyName System.Windows.Forms

$errorFlag = $false

$arch = (Get-ComputerInfo).OsArchitecture
if ($arch -notlike "*64*") { $errorFlag = $true }

$vmSupport = (Get-ComputerInfo).HyperVRequirementVirtualizationFirmwareEnabled
if (-not $vmSupport) { $errorFlag = $true }

$winBuild = [int](Get-ComputerInfo).OsBuildNumber
if ($winBuild -lt 18362) { $errorFlag = $true }

$memGB = ((Get-ComputerInfo).CsTotalPhysicalMemory / 1GB)
if ($memGB -lt 4) { $errorFlag = $true }

$diskFreeGB = ((Get-PSDrive C).Free / 1GB)
if ($diskFreeGB -lt 20) { $errorFlag = $true }

$hyperV = (Get-WindowsOptionalFeature -Online | Where-Object {$_.FeatureName -like "Microsoft-Hyper-V*"}).State
$wsl = (Get-WindowsOptionalFeature -Online | Where-Object {$_.FeatureName -like "*Subsystem-Linux*"}).State
if ($hyperV -ne "Enabled" -or $wsl -ne "Enabled") { $errorFlag = $true }

if ($errorFlag) {
    $message = "❌ 系统不适合安装 Docker Desktop / WSL2"
    $icon = [System.Windows.Forms.MessageBoxIcon]::Error
} else {
    $message = "✅ 系统适合安装 Docker Desktop / WSL2"
    $icon = [System.Windows.Forms.MessageBoxIcon]::Information
}

[System.Windows.Forms.MessageBox]::Show($message, "Docker/WSL 检测结果", [System.Windows.Forms.MessageBoxButtons]::OK, $icon)

效果:客户双击运行脚本 → 直接弹窗 ✅ 或 ❌,完全零门槛。


6️⃣ 打包成 .EXE(可选)

如果客户不想接触脚本,可以用 PS2EXE 工具把脚本打包成 .exe 文件:

Install-Module -Name ps2exe
Invoke-ps2exe .\DockerWSL_Check_GUI.ps1 .\DockerWSL_Check.exe

这样客户直接双击 .exe 就能运行,无需打开 PowerShell。


7️⃣ 总结

✅ 通过这个脚本,你可以 一分钟判断客户电脑能否装 Docker
✅ 支持命令行和图形化两种模式
✅ 可以打包成 .exe 发给客户直接运行


Logo

码道开发者社区,聚焦华为云码道 CodeArts 代码智能体,沉淀 Agent、Skill、鸿蒙开发实战内容,供开发者查阅资料、交流技术、分享工程实践

更多推荐