#requires -Version 5.1 <# .SYNOPSIS 验证「自动更新」全链路(**不卸载重装**):启动当前已安装的旧版本,让它自己升级到后端最新版。 .DESCRIPTION 前置:先用 build.ps1 发布一个**比当前安装版本更高**的包(MSI + version.json 已落到后端)。 脚本做这些事: 1. 从后端 /download/version.json 读目标版本; 2. 与当前安装版本比较,已是最新就直接退出(避免无意义的测试); 3. 关掉正在运行的软件(含单文件自解压出来的真实进程); 4. 启动**旧版本**,并设置 YUNCANG_AUTO_UPDATE=1(更新窗口出现后自动开始下载); 5. 轮询安装目录的 ProductVersion,直到变成目标版本(或超时); 6. 检查本机服务 17300 是否有响应、进程是否已恢复。 .PARAMETER TimeoutSeconds 等更新完成的最长时间,默认 180 秒。 .PARAMETER Backend 后端地址(读 version.json 用),默认 http://127.0.0.1:7300。 .EXAMPLE cd desktop\installer .\build.ps1 -Environment Development -Version 1.0.7 .\test-update.ps1 .NOTES 本文件必须保存为 UTF-8 with BOM(PowerShell 5.1 读无 BOM 脚本按 GBK 解析,中文注释会语法报错)。 #> param( [int]$TimeoutSeconds = 180, [string]$Backend = 'http://127.0.0.1:7300' ) $ErrorActionPreference = 'Stop' function Get-Base([string]$version) { if (-not $version) { return $null } return ($version -split '\+')[0] } $exe = Join-Path $env:LOCALAPPDATA 'Programs\YuncangTrace\YuncangTrace.LocalService.exe' if (-not (Test-Path $exe)) { throw "本机还没安装本软件:$exe(先装一次 MSI 或 EXE)" } $installed = (Get-Item $exe).VersionInfo.ProductVersion $installedBase = Get-Base $installed Write-Host "当前安装版本:$installedBase" # 1) 目标版本 $url = "$($Backend.TrimEnd('/'))/download/version.json?t=$([DateTimeOffset]::UtcNow.ToUnixTimeMilliseconds())" $remote = Invoke-RestMethod -Uri $url -TimeoutSec 10 $target = [string]$remote.version if (-not $target) { throw "version.json 里没有 version 字段" } Write-Host "后端目标版本:$target" Write-Host "下载地址:$($remote.downloadUrl)" if ([version]$installedBase -ge [version]$target) { Write-Host "" Write-Host "当前已不低于目标版本,无需测试。先发布一个更高版本再跑本脚本:" -ForegroundColor Yellow Write-Host " .\build.ps1 -Environment Development -Version <更高版本>" -ForegroundColor Yellow return } # 2) 关掉正在运行的实例(单文件自包含会先解压,进程名相同) Get-CimInstance Win32_Process | Where-Object { $_.Name -like '*Yuncang*' } | ForEach-Object { Stop-Process -Id $_.ProcessId -Force -ErrorAction SilentlyContinue } Start-Sleep -Seconds 2 # 3) 启动旧版本(自动下载) $env:YUNCANG_AUTO_UPDATE = '1' Write-Host "" Write-Host "启动旧版本 $installedBase(自动更新已开启)…" -ForegroundColor Cyan $startedAt = Get-Date Start-Process $exe # 4) 等版本变化 $deadline = (Get-Date).AddSeconds($TimeoutSeconds) $last = $null while ((Get-Date) -lt $deadline) { Start-Sleep -Seconds 3 $current = (Get-Item $exe -ErrorAction SilentlyContinue).VersionInfo.ProductVersion if ($current -ne $last) { $last = $current $spent = [math]::Round(((Get-Date) - $startedAt).TotalSeconds, 1) Write-Host " [${spent}s] 安装版本 = $current" } $base = Get-Base $current if ($base -and [version]$base -eq [version]$target) { break } } # 5) 结果 $final = (Get-Item $exe).VersionInfo.ProductVersion $finalBase = Get-Base $final $elapsed = [math]::Round(((Get-Date) - $startedAt).TotalSeconds, 1) Write-Host "" if ([version]$finalBase -eq [version]$target) { Write-Host "✔ 自动更新完成:$installedBase → $finalBase(耗时 ${elapsed}s)" -ForegroundColor Green } else { Write-Host "✘ 自动更新未完成:当前 $finalBase,目标 $target(已等 ${TimeoutSeconds}s)" -ForegroundColor Red } # 6) 服务与进程(新进程是 msiexec 装完后再拉起的,可能比版本变化晚几秒 → 轮询等一会儿) $probeDeadline = (Get-Date).AddSeconds(60) $localCode = $null $processes = @() while ((Get-Date) -lt $probeDeadline) { $localCode = & curl.exe -s -o NUL -w "%{http_code}" --max-time 3 "http://127.0.0.1:17300/api/query/" $processes = @(Get-CimInstance Win32_Process | Where-Object { $_.Name -like '*Yuncang*' }) if ($processes.Count -gt 0 -and $localCode -and $localCode -ne '000') { break } Start-Sleep -Seconds 3 } if ($localCode -and $localCode -ne '000') { Write-Host "✔ 本机服务 17300 有响应(HTTP $localCode)" -ForegroundColor Green } else { Write-Host "✘ 本机服务 17300 无响应" -ForegroundColor Red } if ($processes.Count -gt 0) { Write-Host "✔ 软件进程已恢复(PID $($processes[0].ProcessId))" -ForegroundColor Green } else { Write-Host "✘ 没找到软件进程" -ForegroundColor Red }