Issue
I am trying to write a small putty replacement using powershell. I am having trouble passing the SSH command in, here is what I have:
The outgrid opens and I click on a server, but its not connecting. I get a new Windows terminal window with this text:
[error 2147942402 (0x80070002) when launching
"ssh root@server1"']`
I would like it to open a new tab in my existing WT session, and pass ssh root@server, then I can enter the PW and connect.
Any help would be great, Thanks!
# ssh-connect.ps1
$servers = @(
"server1",
"server2",
"server3"
)
# Create an array to store the data for Out-GridView
$data = @()
foreach ($server in $servers) {
$data += [PSCustomObject]@{
Server = $server
}
}
$selectedServer = $data | Out-GridView -Title "Select a server" -PassThru
if ($selectedServer) {
# Construct the SSH command
$sshCommand = "ssh root@$($selectedServer.Server)"
# Execute the Windows Terminal command using the call operator
& wt new-tab -p PowerShell -d . -H $sshCommand
}
Solution
Do not construct your SSH command as a single string, because PowerShell passes it as a single argument (enclosed in "..."
) to wt.exe
, the Windows Terminal CLI, whereas the latter expects a command (line) to be specified as individual arguments.
Therefore:
# Note how ssh and its argument are specified as separate arguments.
wt new-tab -p PowerShell `; split-pane -H ssh "root@$($selectedServer.Server)"
Note:
-d .
was omitted, because it is redundant (the caller's current directory is used by default).In order to use
-H
to create a horizontally split pane, you need thesplit-pane
subcommand.Multiple
wt.exe
subcommands must be separated with;
and because;
is also a PowerShell metacharacter, it must be escaped as`;
(';'
or";"
would work too).The lower horizontal pane created with
split-pane
uses your default Windows Terminal in terms of styling and doesn't actually create a shell session - it executes the command given directly in the pane.To use the styling of a different profile, simply use another
-p
argument, in the context of thesplit-pane
subcommand; e.g.:wt new-tab -p PowerShell `; split-pane -H -p PowerShell ssh "root@$($selectedServer.Server)"
Answered By - mklement0 Answer Checked By - Marilyn (WPSolving Volunteer)