Files
openclaw-setup-cn/install-windows.ps1

260 lines
10 KiB
PowerShell
Raw Permalink 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.
# ─────────────────────────────────────────────
# OpenClaw 一键部署安装脚本 (Windows)
# 安装 Node.js + OpenClaw原生 Windows 运行
# ─────────────────────────────────────────────
function Write-Info($msg) { Write-Host "[信息] $msg" -ForegroundColor Cyan }
function Write-Ok($msg) { Write-Host "[完成] $msg" -ForegroundColor Green }
function Write-Warn($msg) { Write-Host "[警告] $msg" -ForegroundColor Yellow }
function Write-Err($msg) { Write-Host "[错误] $msg" -ForegroundColor Red; throw $msg }
function Write-Header {
Write-Host ""
Write-Host "========================================" -ForegroundColor White
Write-Host " OpenClaw 一键部署安装器 (Windows)" -ForegroundColor White
Write-Host "========================================" -ForegroundColor White
Write-Host ""
}
try {
Write-Header
Write-Host "请选择操作:" -ForegroundColor White
Write-Host " 1) 安装 / 更新 OpenClaw" -ForegroundColor Gray
Write-Host " 2) 卸载 OpenClaw" -ForegroundColor Gray
$action = Read-Host "请输入选项 [1/2](默认 1"
if ([string]::IsNullOrWhiteSpace($action)) { $action = "1" }
if ($action -eq "2") {
Write-Info "你选择了:卸载 OpenClaw"
$uninstalled = $false
if (Get-Command openclaw -ErrorAction SilentlyContinue) {
try {
openclaw uninstall --all --yes --non-interactive
$uninstalled = $true
} catch {
Write-Warn "openclaw 卸载失败,尝试 npx 方式..."
}
}
if (-not $uninstalled -and (Get-Command npx -ErrorAction SilentlyContinue)) {
try {
npx -y openclaw uninstall --all --yes --non-interactive
$uninstalled = $true
} catch {
Write-Warn "npx 卸载失败。"
}
}
if (Get-Command npm -ErrorAction SilentlyContinue) {
try { npm uninstall -g openclaw 2>$null | Out-Null } catch {}
}
if ($uninstalled) {
Write-Ok "OpenClaw 已卸载。"
} else {
Write-Err "未能完成 OpenClaw 卸载。请确认 Node.js/npm 可用后重试。"
}
return
}
# ─────────────────────────────────────────────
# 步骤 1: 检查 / 安装 Node.js 22+
# ─────────────────────────────────────────────
Write-Info "正在检查 Node.js..."
$needsNode = $true
try {
$nodeVersion = (node -v 2>$null)
if ($nodeVersion -match "v(\d+)") {
$major = [int]$Matches[1]
if ($major -ge 22) {
Write-Ok "已找到 Node.js $nodeVersion"
$needsNode = $false
} else {
Write-Warn "已找到 Node.js $nodeVersion,但需要 v22+,正在升级..."
}
}
} catch {
Write-Warn "未找到 Node.js。"
}
if ($needsNode) {
Write-Info "正在安装 Node.js 22..."
$installed = $false
# 方式 1: wingetWindows 11 / Windows 10 自带)
if (-not $installed -and (Get-Command winget -ErrorAction SilentlyContinue)) {
Write-Info "使用 winget 安装..."
winget install OpenJS.NodeJS.LTS --accept-package-agreements --accept-source-agreements 2>$null
if ($LASTEXITCODE -eq 0) {
$installed = $true
Write-Ok "Node.js 已通过 winget 安装"
}
}
# 方式 2: Chocolatey
if (-not $installed -and (Get-Command choco -ErrorAction SilentlyContinue)) {
Write-Info "使用 Chocolatey 安装..."
choco install nodejs-lts -y 2>$null
if ($LASTEXITCODE -eq 0) {
$installed = $true
Write-Ok "Node.js 已通过 Chocolatey 安装"
}
}
# 方式 3: 手动下载提示
if (-not $installed) {
Write-Err "无法自动安装 Node.js。请手动下载安装`n`n 下载地址https://nodejs.cn/download/`nhttps://nodejs.org/en/download/`n`n 安装完成后重新打开 PowerShell 运行此脚本。"
}
# 刷新 PATH
$env:Path = [System.Environment]::GetEnvironmentVariable("Path", "Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path", "User")
# 验证安装
try {
$nodeVersion = (node -v 2>$null)
Write-Ok "Node.js $nodeVersion 安装成功"
} catch {
Write-Err "Node.js 安装后验证失败。请关闭 PowerShell 重新打开后再试。"
}
}
# ─────────────────────────────────────────────
# 步骤 2: 检查 / 安装 Git
# ─────────────────────────────────────────────
Write-Info "正在检查 Git..."
if (Get-Command git -ErrorAction SilentlyContinue) {
Write-Ok "已找到 Git。"
} else {
Write-Info "正在安装 GitOpenClaw 依赖需要)..."
$gitInstalled = $false
if (-not $gitInstalled -and (Get-Command winget -ErrorAction SilentlyContinue)) {
Write-Info "使用 winget 安装 Git..."
winget install Git.Git --accept-package-agreements --accept-source-agreements 2>$null
if ($LASTEXITCODE -eq 0) {
$gitInstalled = $true
Write-Ok "Git 已通过 winget 安装"
}
}
if (-not $gitInstalled -and (Get-Command choco -ErrorAction SilentlyContinue)) {
Write-Info "使用 Chocolatey 安装 Git..."
choco install git -y 2>$null
if ($LASTEXITCODE -eq 0) {
$gitInstalled = $true
Write-Ok "Git 已通过 Chocolatey 安装"
}
}
# 方式 3: 直接下载安装
if (-not $gitInstalled) {
Write-Info "正在直接下载 Git 安装包..."
$gitInstaller = Join-Path $env:TEMP "git-installer.exe"
try {
# 从 GitHub 获取最新 64-bit 安装包 URL
$releases = Invoke-RestMethod "https://api.github.com/repos/git-for-windows/git/releases/latest" -ErrorAction Stop
$asset = $releases.assets | Where-Object { $_.name -match "Git-.*-64-bit\.exe$" } | Select-Object -First 1
if ($asset) {
Invoke-WebRequest -Uri $asset.browser_download_url -OutFile $gitInstaller -ErrorAction Stop
Write-Info "正在静默安装 Git..."
Start-Process -FilePath $gitInstaller -ArgumentList "/VERYSILENT", "/NORESTART", "/NOCANCEL", "/SP-", "/CLOSEAPPLICATIONS", "/RESTARTAPPLICATIONS" -Wait
Remove-Item $gitInstaller -Force -ErrorAction SilentlyContinue
$gitInstalled = $true
Write-Ok "Git 已通过直接下载安装"
}
} catch {
Write-Warn "直接下载安装失败: $_"
}
}
if (-not $gitInstalled) {
Write-Err "无法自动安装 Git。请手动下载安装`n`n 下载地址https://git-scm.com/download/win`n`n 安装完成后重新打开 PowerShell 运行此脚本。"
}
# 刷新 PATH
$env:Path = [System.Environment]::GetEnvironmentVariable("Path", "Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path", "User")
}
# ─────────────────────────────────────────────
# 步骤 3: 安装 / 更新 OpenClaw
# 国内网络优先npmmirror → npmjs必要时
# ─────────────────────────────────────────────
Write-Info "正在安装 / 更新 OpenClaw..."
function Install-OpenClawByRegistry($registry) {
Write-Info "尝试通过 npm 安装 OpenClaw$registry..."
npm install -g openclaw@latest --registry=$registry 2>&1 | Write-Host
return ($LASTEXITCODE -eq 0)
}
$installedOpenClaw = $false
if (Install-OpenClawByRegistry "https://registry.npmmirror.com") {
Write-Ok "已通过 npmmirror 安装 / 更新 OpenClaw。"
$installedOpenClaw = $true
} elseif (Install-OpenClawByRegistry "https://registry.npmjs.org") {
Write-Ok "已通过 npm 官方源安装 / 更新 OpenClaw。"
$installedOpenClaw = $true
}
if (-not $installedOpenClaw) {
Write-Err "OpenClaw 安装失败npmmirror 与 npm 官方源均不可用。"
}
# 刷新 PATH
$env:Path = [System.Environment]::GetEnvironmentVariable("Path", "Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path", "User")
# 把 npm global bin 加到 PATH
try {
$npmPrefix = (npm config get prefix 2>$null).Trim()
if ($npmPrefix) {
$userPath = [Environment]::GetEnvironmentVariable("Path", "User")
if (-not ($userPath -split ";" | Where-Object { $_ -ieq $npmPrefix })) {
[Environment]::SetEnvironmentVariable("Path", "$userPath;$npmPrefix", "User")
$env:Path += ";$npmPrefix"
}
}
} catch {}
Write-Ok "OpenClaw 安装 / 更新完成。"
# ─────────────────────────────────────────────
# 步骤 3: 启动交互式配置
# ─────────────────────────────────────────────
Write-Host ""
Write-Host "========================================" -ForegroundColor Green
Write-Host " OpenClaw 安装完成!开始配置..." -ForegroundColor Green
Write-Host "========================================" -ForegroundColor Green
Write-Host ""
openclaw onboard --accept-risk --flow quickstart --install-daemon --gateway-bind loopback --node-manager npm --skip-search --skip-channels --skip-skills
Write-Host ""
Write-Host "========================================" -ForegroundColor Green
Write-Host " 配置完成!" -ForegroundColor Green
Write-Host "========================================" -ForegroundColor Green
Write-Host ""
Write-Host " 接下来:" -ForegroundColor White
Write-Host " 1. 开始使用 OpenClawopenclaw" -ForegroundColor Gray
Write-Host " 2. 查看网关状态openclaw gateway status" -ForegroundColor Gray
Write-Host " 3. 如需卸载iwr -useb https://github.carrydelahaye.work/openclaw/openclaw-setup-cn/raw/branch/main/uninstall-windows.ps1 | iex" -ForegroundColor Gray
Write-Host ""
} catch {
Write-Host ""
Write-Host "[错误] 脚本执行失败: $_" -ForegroundColor Red
Write-Host ""
} finally {
Write-Host ""
Read-Host "按 Enter 键关闭此窗口"
}