From 5e9d63ae1aa02e678c86fc6e3d50527f2ecbdc9c Mon Sep 17 00:00:00 2001 From: Rick Mu Date: Fri, 13 Feb 2026 18:58:56 +1100 Subject: [PATCH] Fix PowerShell window closing: wrap in try/catch/finally - Remove $ErrorActionPreference = "Stop" (caused unhandled exits) - Write-Err now throws to outer catch (shows error, stays open) - finally block always pauses with Read-Host before closing - npm output piped through Write-Host to prevent stream errors Co-Authored-By: Claude Opus 4.6 --- configure-aws-bedrock.ps1 | 15 ++++++++++++--- install-windows-en.ps1 | 22 +++++++++++++++------- install-windows.ps1 | 24 ++++++++++++++++-------- 3 files changed, 43 insertions(+), 18 deletions(-) diff --git a/configure-aws-bedrock.ps1 b/configure-aws-bedrock.ps1 index a55f5db..9eea1b9 100644 --- a/configure-aws-bedrock.ps1 +++ b/configure-aws-bedrock.ps1 @@ -2,12 +2,12 @@ # OpenClaw AWS Bedrock 配置脚本 (Windows) # ───────────────────────────────────────────── -$ErrorActionPreference = "Stop" - 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; Read-Host "按 Enter 键退出"; return } +function Write-Err($msg) { Write-Host "[错误] $msg" -ForegroundColor Red; throw $msg } + +try { Write-Host "" Write-Host "========================================" -ForegroundColor White @@ -235,3 +235,12 @@ Write-Host " * bedrock:InvokeModel" -ForegroundColor Yellow Write-Host " * bedrock:InvokeModelWithResponseStream" -ForegroundColor Yellow Write-Host " 或使用托管策略:AmazonBedrockFullAccess" -ForegroundColor Yellow Write-Host "" + +} catch { + Write-Host "" + Write-Host "[错误] 脚本执行失败: $_" -ForegroundColor Red + Write-Host "" +} finally { + Write-Host "" + Read-Host "按 Enter 键关闭此窗口" +} diff --git a/install-windows-en.ps1 b/install-windows-en.ps1 index 3024569..68bb54c 100644 --- a/install-windows-en.ps1 +++ b/install-windows-en.ps1 @@ -3,12 +3,10 @@ # Installs Node.js + OpenClaw, runs natively # ───────────────────────────────────────────── -$ErrorActionPreference = "Stop" - function Write-Info($msg) { Write-Host "[INFO] $msg" -ForegroundColor Cyan } function Write-Ok($msg) { Write-Host "[DONE] $msg" -ForegroundColor Green } function Write-Warn($msg) { Write-Host "[WARN] $msg" -ForegroundColor Yellow } -function Write-Err($msg) { Write-Host "[ERROR] $msg" -ForegroundColor Red; Read-Host "Press Enter to exit"; return } +function Write-Err($msg) { Write-Host "[ERROR] $msg" -ForegroundColor Red; throw $msg } function Write-Header { Write-Host "" @@ -18,6 +16,8 @@ function Write-Header { Write-Host "" } +try { + Write-Header # ───────────────────────────────────────────── @@ -50,7 +50,7 @@ if ($needsNode) { # Method 1: winget (built into Windows 11 / Windows 10) if (-not $installed -and (Get-Command winget -ErrorAction SilentlyContinue)) { Write-Info "Installing via winget..." - winget install OpenJS.NodeJS.LTS --accept-package-agreements --accept-source-agreements + winget install OpenJS.NodeJS.LTS --accept-package-agreements --accept-source-agreements 2>$null if ($LASTEXITCODE -eq 0) { $installed = $true Write-Ok "Node.js installed via winget" @@ -60,7 +60,7 @@ if ($needsNode) { # Method 2: Chocolatey if (-not $installed -and (Get-Command choco -ErrorAction SilentlyContinue)) { Write-Info "Installing via Chocolatey..." - choco install nodejs-lts -y + choco install nodejs-lts -y 2>$null if ($LASTEXITCODE -eq 0) { $installed = $true Write-Ok "Node.js installed via Chocolatey" @@ -69,7 +69,6 @@ if ($needsNode) { # Method 3: Manual download prompt if (-not $installed) { - Write-Host "" Write-Err "Could not install Node.js automatically. Please install manually and re-run this script:`n`n Download: https://nodejs.org/en/download/`n`n After installing, reopen PowerShell and run this script again." } @@ -101,7 +100,7 @@ try { } if ($needsInstall) { - npm install -g openclaw@latest + npm install -g openclaw@latest 2>&1 | Write-Host if ($LASTEXITCODE -ne 0) { Write-Err "OpenClaw installation failed. Please run manually: npm install -g openclaw@latest" } @@ -150,3 +149,12 @@ Write-Host " 2. Start OpenClaw: openclaw" -ForegroundColor Gray Write-Host " 3. Add skills (optional): openclaw configure --section skills" -ForegroundColor Gray Write-Host " 4. Browse available skills: openclaw skills" -ForegroundColor Gray Write-Host "" + +} catch { + Write-Host "" + Write-Host "[ERROR] Script failed: $_" -ForegroundColor Red + Write-Host "" +} finally { + Write-Host "" + Read-Host "Press Enter to close this window" +} diff --git a/install-windows.ps1 b/install-windows.ps1 index 13b9b18..086c39f 100644 --- a/install-windows.ps1 +++ b/install-windows.ps1 @@ -3,12 +3,10 @@ # 安装 Node.js + OpenClaw,原生 Windows 运行 # ───────────────────────────────────────────── -$ErrorActionPreference = "Stop" - 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; Read-Host "按 Enter 键退出"; return } +function Write-Err($msg) { Write-Host "[错误] $msg" -ForegroundColor Red; throw $msg } function Write-Header { Write-Host "" @@ -18,6 +16,8 @@ function Write-Header { Write-Host "" } +try { + Write-Header # ───────────────────────────────────────────── @@ -50,7 +50,7 @@ if ($needsNode) { # 方式 1: winget(Windows 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 + winget install OpenJS.NodeJS.LTS --accept-package-agreements --accept-source-agreements 2>$null if ($LASTEXITCODE -eq 0) { $installed = $true Write-Ok "Node.js 已通过 winget 安装" @@ -60,7 +60,7 @@ if ($needsNode) { # 方式 2: Chocolatey if (-not $installed -and (Get-Command choco -ErrorAction SilentlyContinue)) { Write-Info "使用 Chocolatey 安装..." - choco install nodejs-lts -y + choco install nodejs-lts -y 2>$null if ($LASTEXITCODE -eq 0) { $installed = $true Write-Ok "Node.js 已通过 Chocolatey 安装" @@ -69,8 +69,7 @@ if ($needsNode) { # 方式 3: 手动下载提示 if (-not $installed) { - Write-Host "" - Write-Err "无法自动安装 Node.js。请手动安装后重新运行此脚本:`n`n 下载地址:https://nodejs.cn/download/`n 或:https://nodejs.org/en/download/`n`n 安装完成后重新打开 PowerShell 运行此脚本。" + Write-Err "无法自动安装 Node.js。请手动下载安装:`n`n 下载地址:https://nodejs.cn/download/`n 或:https://nodejs.org/en/download/`n`n 安装完成后重新打开 PowerShell 运行此脚本。" } # 刷新 PATH @@ -101,7 +100,7 @@ try { } if ($needsInstall) { - npm install -g openclaw@latest + npm install -g openclaw@latest 2>&1 | Write-Host if ($LASTEXITCODE -ne 0) { Write-Err "OpenClaw 安装失败。请手动运行:npm install -g openclaw@latest" } @@ -150,3 +149,12 @@ Write-Host " 2. 开始使用 OpenClaw:openclaw" -ForegroundColor Gray Write-Host " 3. 添加技能(可选):openclaw configure --section skills" -ForegroundColor Gray Write-Host " 4. 浏览可用技能:openclaw skills" -ForegroundColor Gray Write-Host "" + +} catch { + Write-Host "" + Write-Host "[错误] 脚本执行失败: $_" -ForegroundColor Red + Write-Host "" +} finally { + Write-Host "" + Read-Host "按 Enter 键关闭此窗口" +}