<# .SYNOPSIS OA vault save - 一次性脚本 (v2: Read-Host 兼容版) .DESCRIPTION 端到端 Pattern B vault 链路实测脚本 (v2): 1. 防御性清理 agent-browser daemon 2. 启动 daemon (带 --args --no-sandbox) 3. Read-Host 接收 username (明文回显) 4. Read-Host -AsSecureString 接收 password (不回显) 5. 保存到 vault 名 "oa" 6. 验证 vault (auth list) 7. 清理敏感变量 .NOTES Author: Duckula Date : 2026-07-29 (v2) Why : Get-Credential 在 hosted PowerShell 上下文渲染失败 改用 Read-Host + AsSecureString 兼容任何交互式 shell - Read-Host 明文 (回显) for username - Read-Host -AsSecureString (SecureString, 不回显) for password #> # 1. 清理 daemon Write-Host "============================================" -ForegroundColor Cyan Write-Host " OA vault save - Pattern B 端到端测试" -ForegroundColor Cyan Write-Host "============================================" -ForegroundColor Cyan Write-Host "" Write-Host "Step 1: 清理 agent-browser daemon (5s)" -ForegroundColor Yellow agent-browser close --all 2>&1 | Out-Null for ($i = 1; $i -le 5; $i++) { $procs = Get-Process | Where-Object { $_.Name -match '^(chrome|agent-browser-win32-x64)$' } if ($procs.Count -eq 0) { break } foreach ($p in $procs) { try { & taskkill /F /PID $p.Id /T 2>&1 | Out-Null } catch {} } Start-Sleep 2 } # 2. 启动 daemon Write-Host "" Write-Host "Step 2: 启动 daemon (--args --no-sandbox)" -ForegroundColor Yellow $sw = [System.Diagnostics.Stopwatch]::StartNew() agent-browser --args --no-sandbox open "https://oa.servyou-it.com/" 2>&1 | Out-Null $sw.Stop() Write-Host " [open] $($sw.ElapsedMilliseconds) ms" -ForegroundColor Gray Start-Sleep 5 # 3. Read-Host 接收凭据 Write-Host "" Write-Host "Step 3: 输入 OA 账号" -ForegroundColor Yellow Write-Host " (Username 回显, Password 不回显)" -ForegroundColor Cyan Write-Host "" $username = Read-Host " OA Username (工号)" if ([string]::IsNullOrWhiteSpace($username)) { Write-Host " ❌ Username empty" -ForegroundColor Red exit 1 } Write-Host "" $securePwd = Read-Host " OA Password" -AsSecureString if ($null -eq $securePwd) { Write-Host " ❌ Password empty" -ForegroundColor Red exit 1 } $BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($securePwd) $password = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR) [System.Runtime.InteropServices.Marshal]::ZeroFreeBSTR($BSTR) if ([string]::IsNullOrWhiteSpace($password)) { Write-Host " ❌ Password empty" -ForegroundColor Red exit 1 } Write-Host " ✅ Credentials received" -ForegroundColor Green Write-Host " Username: $username" -ForegroundColor Gray Write-Host " Password: *** (length=$(($password.Length)))" -ForegroundColor Gray # 4. 保存到 vault Write-Host "" Write-Host "Step 4: auth save oa" -ForegroundColor Yellow $password | agent-browser auth save oa ` --url "https://oa.servyou-it.com/" ` --username $username ` --password-stdin 2>&1 | Out-Null if ($LASTEXITCODE -ne 0) { Write-Host " ❌ auth save failed (exit=$LASTEXITCODE)" -ForegroundColor Red exit 1 } # 5. 验证 Write-Host "" Write-Host "Step 5: verify vault" -ForegroundColor Yellow agent-browser auth list 2>&1 | Out-Null # 6. 清理敏感变量 $username = $null $password = $null [System.GC]::Collect() Write-Host "" Write-Host "============================================" -ForegroundColor Green Write-Host " ✅ Vault 'oa' saved" -ForegroundColor Green Write-Host "============================================" -ForegroundColor Green