You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用PowerShell向Windows Terminal传递SSH参数失败的问题排查

问题解决:PowerShell SSH连接脚本错误修复

错误原因

你遇到的error 2147942402 (0x80070002)是因为使用了Windows Terminal不支持的-H参数,导致命令解析失败,系统无法识别要启动的命令。

修正后的脚本

# ssh-connect.ps1
$servers = @(
    "server1",
    "server2",
    "server3"
)

# 构造Out-GridView的数据源
$data = $servers | ForEach-Object {
    [PSCustomObject]@{
        Server = $_
    }
}

$selectedServer = $data | Out-GridView -Title "选择服务器" -PassThru
if ($selectedServer) {
    $sshCommand = "ssh root@$($selectedServer.Server)"
    # 在当前Windows Terminal窗口新建标签,执行SSH命令并保持会话
    & wt new-tab -p "PowerShell" -d . --command "powershell -NoExit -Command `"$sshCommand`""
}

关键修改点

  • 移除无效的-H参数,改用Windows Terminal标准的--command参数指定启动标签时要执行的命令。
  • 用powershell -NoExit -Command包裹SSH命令:-NoExit确保执行SSH后会话不会立即关闭,让你能输入密码完成连接;嵌套的双引号"用来处理PowerShell的字符串解析问题。
  • 若需要强制在当前Windows Terminal窗口新建标签(即使脚本在外部运行),可添加-w 0参数,修改为:
    & wt -w 0 new-tab -p "PowerShell" -d . --command "powershell -NoExit -Command `"$sshCommand`""
    
    其中0代表当前活动的Windows Terminal窗口。

额外说明

如果你的Windows Terminal配置的PowerShell是PowerShell Core(pwsh),请将脚本中的powershell替换为pwsh。

内容的提问来源于stack exchange,提问作者Matt

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.03 10:52:19