使用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
相关产品推荐
相关产品推荐

