<#
.SYNOPSIS
OpenClaw Windows 一键安装脚本
.DESCRIPTION
本脚本包含OpenClaw环境检查、依赖安装、配置设置和服务启动的完整流程,
解决了Windows环境下常见的安装问题,如命令不兼容、配置迁移等。
#>

# 1. 检查并设置执行策略
if ((Get-ExecutionPolicy -Scope CurrentUser) -ne "RemoteSigned") {
    Write-Host "🔧 正在设置PowerShell执行策略..." -ForegroundColor Cyan
    Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser -Force
}

# 2. 检查Node.js版本
$nodeVersion = node -v 2>&1
if ($LASTEXITCODE -ne 0) {
    Write-Host "⚠️  Node.js未安装,将自动安装..." -ForegroundColor Yellow
    # 下载并安装Node.js 22.x
    $nodeInstaller = "$env:TEMP\node-v22.11.0-x64.msi"
    Invoke-WebRequest -Uri "https://nodejs.org/dist/v22.11.0/node-v22.11.0-x64.msi"  -OutFile $nodeInstaller
    Start-Process msiexec.exe -ArgumentList "/i `"$nodeInstaller`" /qn ADDLOCAL=ALL" -Wait
    Remove-Item $nodeInstaller -Force
    # 刷新环境变量
    $env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User")
} else {
    $nodeVersionNumber = [version]$nodeVersion.Substring(1)
    if ($nodeVersionNumber -lt [version]"22.0.0") {
        Write-Host "⚠️  Node.js版本过低($nodeVersion),需要升级到22.0.0以上" -ForegroundColor Yellow
        Write-Host "请访问 https://nodejs.org/zh-cn/download  下载并安装最新版本" -ForegroundColor Cyan
        exit 1
    }
}

# 3. 配置国内镜像源(加速下载)
Write-Host "🔧 正在配置国内镜像源..." -ForegroundColor Cyan
npm config set registry https://registry.npmmirror.com/  -g

# 4. 全局安装OpenClaw
Write-Host "📦 正在安装OpenClaw..." -ForegroundColor Cyan
npm install -g openclaw-cn@latest --force

# 5. 检查安装结果
$openclawVersion = openclaw -v 2>&1
if ($LASTEXITCODE -ne 0) {
    Write-Host "❌ OpenClaw安装失败,请检查网络连接并重新运行脚本" -ForegroundColor Red
    exit 1
}

# 6. 创建配置文件目录
$configDir = "$env:USERPROFILE\.openclaw"
if (-not (Test-Path $configDir)) {
    New-Item -ItemType Directory -Path $configDir -Force | Out-Null
}

# 7. 生成随机令牌
$token = -join ((65..90) + (97..122) + (48..57) | Get-Random -Count 24 | % {[char]$_})

# 8. 创建配置文件
Write-Host "🔧 正在创建配置文件..." -ForegroundColor Cyan
@"
{
  "gateway": {
    "mode": "local",
    "port": 18789,
    "auth": {
      "token": "$token"
    }
  },
  "agent": {
    "model": "anthropic/claude-opus-4-6"
  },
  "plugins": {
    "dingtalk": {},
    "feishu": {},
    "feishu_calendar": {}
  }
}
"@ | Out-File "$configDir\openclaw.json" -Encoding UTF8

# 9. 显示安装结果
Write-Host "🎉 OpenClaw安装成功!" -ForegroundColor Green
Write-Host ""
Write-Host "📋 当前配置:" -ForegroundColor Yellow
Write-Host "  OpenClaw版本: $openclawVersion"
Write-Host "  Node.js版本: $nodeVersion"
Write-Host "  网关端口: 18789"
Write-Host "  认证令牌: $token"
Write-Host "  配置文件: $configDir\openclaw.json"
Write-Host ""
Write-Host "🚀 常用命令:" -ForegroundColor Yellow
Write-Host "  启动网关: openclaw gateway start --port 18789"
Write-Host "  查看配置: openclaw config"
Write-Host "  查看状态: openclaw gateway status"
Write-Host "  访问Dashboard: http://127.0.0.1:18789/?token=$token"
Write-Host ""
Write-Host "💡 提示:使用openclaw gateway start --port 18789启动网关,然后访问上面的Dashboard地址" -ForegroundColor Cyan

<#
.SYNOPSIS
OpenClaw网关令牌管理脚本
.DESCRIPTION
本脚本包含令牌生成、修改、备份和自动打开带令牌的Dashboard等功能
#>

# 定义配置文件路径
$configFile = "$env:USERPROFILE\.openclaw\openclaw.json"
$backupDir = "$env:USERPROFILE\.openclaw\backups"

# 创建备份目录
if (-not (Test-Path $backupDir)) {
    New-Item -ItemType Directory -Path $backupDir -Force | Out-Null
}

# 显示菜单
function Show-Menu {
    Clear-Host
    Write-Host "🦞 OpenClaw网关令牌管理脚本" -ForegroundColor Green
    Write-Host "=============================" -ForegroundColor Cyan
    Write-Host "1. 显示当前令牌"
    Write-Host "2. 生成新的随机令牌"
    Write-Host "3. 手动设置令牌"
    Write-Host "4. 备份当前配置"
    Write-Host "5. 打开带令牌的Dashboard"
    Write-Host "6. 退出"
    Write-Host "=============================" -ForegroundColor Cyan
}

# 显示当前令牌
function Show-CurrentToken {
    try {
        $token = openclaw config get gateway.auth.token
        Write-Host "当前网关令牌: $token" -ForegroundColor Yellow
    } catch {
        Write-Host "❌ 无法获取当前令牌,请检查配置文件是否存在" -ForegroundColor Red
    }
}

# 生成新的随机令牌
function Generate-NewToken {
    try {
        $newToken = -join ((65..90) + (97..122) + (48..57) | Get-Random -Count 24 | % {[char]$_})
        openclaw config set gateway.auth.token $newToken
        Write-Host "🎉 新令牌已生成: $newToken" -ForegroundColor Green
        Write-Host "🔧 网关服务将在5秒后重启..." -ForegroundColor Cyan
        Start-Sleep -Seconds 5
        openclaw gateway restart --port 18789
    } catch {
        Write-Host "❌ 生成新令牌失败,请检查权限" -ForegroundColor Red
    }
}

# 手动设置令牌
function Set-ManualToken {
    try {
        $manualToken = Read-Host "请输入新的网关令牌"
        if (-not [string]::IsNullOrEmpty($manualToken)) {
            openclaw config set gateway.auth.token $manualToken
            Write-Host "🎉 令牌已设置为: $manualToken" -ForegroundColor Green
            Write-Host "🔧 网关服务将在5秒后重启..." -ForegroundColor Cyan
            Start-Sleep -Seconds 5
            openclaw gateway restart --port 18789
        } else {
            Write-Host "❌ 令牌不能为空" -ForegroundColor Red
        }
    } catch {
        Write-Host "❌ 设置令牌失败,请检查权限" -ForegroundColor Red
    }
}

# 备份当前配置
function Backup-Config {
    try {
        $timestamp = Get-Date -Format "yyyyMMdd_HHmmss"
        $backupFile = "$backupDir\openclaw_backup_$timestamp.json"
        Copy-Item $configFile $backupFile -Force
        Write-Host "✅ 配置已备份到: $backupFile" -ForegroundColor Green
    } catch {
        Write-Host "❌ 备份失败,请检查文件是否存在" -ForegroundColor Red
    }
}

# 打开带令牌的Dashboard
function Open-Dashboard {
    try {
        $token = openclaw config get gateway.auth.token
        $dashboardUrl = "http://127.0.0.1:18789/?token=$token"
        Write-Host "🔗 正在打开Dashboard: $dashboardUrl" -ForegroundColor Cyan
        Start-Process $dashboardUrl
    } catch {
        Write-Host "❌ 无法打开Dashboard,请检查网关是否已启动" -ForegroundColor Red
    }
}

# 主程序
do {
    Show-Menu
    $choice = Read-Host "请输入选择(1-6)"
    switch ($choice) {
        1 { Show-CurrentToken; Read-Host "按任意键继续" }
        2 { Generate-NewToken; Read-Host "按任意键继续" }
        3 { Set-ManualToken; Read-Host "按任意键继续" }
        4 { Backup-Config; Read-Host "按任意键继续" }
        5 { Open-Dashboard; Read-Host "按任意键继续" }
        6 { Write-Host "再见!" -ForegroundColor Green; exit }
        default { Write-Host "❌ 无效选择,请重新输入" -ForegroundColor Red; Read-Host "按任意键继续" }
    }
} while ($true)

<#
.SYNOPSIS
OpenClaw开机自启动配置脚本(官方推荐方案)
.DESCRIPTION
使用官方命令创建Windows系统服务,实现开机自动启动
#>

# 1. 配置Gateway模式为本地(本地部署必填)
openclaw config set gateway.mode local

# 2. 安装Gateway服务(创建Windows计划任务,实现登录自动启动)
openclaw gateway install

# 3. 验证服务安装
Get-ScheduledTask -TaskName "OpenClaw Gateway"

# 4. 启动服务
Start-ScheduledTask -TaskName "OpenClaw Gateway"

# 5. 验证服务状态
Get-ScheduledTask -TaskName "OpenClaw Gateway" | Select-Object TaskName, State

# 6. 显示安装结果
Write-Host "🎉 OpenClaw开机自启动服务已成功安装!" -ForegroundColor Green
Write-Host "📋 服务信息:" -ForegroundColor Yellow
Write-Host "  任务名称: OpenClaw Gateway"
Write-Host "  触发条件: 用户登录时"
Write-Host "  执行命令: openclaw gateway start --port 18789"
Write-Host ""
Write-Host "💡 管理命令:" -ForegroundColor Yellow
Write-Host "  停止服务: Stop-ScheduledTask -TaskName 'OpenClaw Gateway'"
Write-Host "  重启服务: Restart-ScheduledTask -TaskName 'OpenClaw Gateway'"
Write-Host "  卸载服务: openclaw gateway uninstall"

环境要求:

1. Node.js 运行环境版本 >= 22.0.0 2. npm 包管理器(随 Node.js 自动安装)点击此处https://nodejs.org/ 下载3. 国内大模型平台的 API Key(智谱 GLM / Minimax / 月之暗面 / 通义千问等) 4. Git 版本控制工具:安装过程中必需使用,点击此处https://git-scm.com/下载,
,5.window版本22H2以上,内部19045.6456

Logo

Agent 垂直技术社区,欢迎活跃、内容共建。

更多推荐