Thursday, October 27, 2022

[SOLVED] Open pwsh with command from cmd without exiting

Issue

I am trying to start a Powershell window that starts an ssh session with the following command:

pwsh.exe -noexit -Command {ssh <username>@<host>}

This works when executed from a pwsh.exe window itself: enter image description here but when executed from cmd, the run window (Win+R) or Task Scheduler (what I need it for), it just displays the command that should have been executed and starts pwsh: enter image description here

That is, the command that doesn't work outside of PowerShell is:

pwsh.exe -noexit -Command {ssh username@host}

The commands have of course been tested with actual websites where, again, 1 works and 2 doesn't.
Any help would be greatly appreciated! Thank you!


Solution

See the -Command parameter in the pwsh documentation. cmd.exe doesn't know the ScriptBlock concept and interprets {ssh test@test} as a string.

When triggering pwsh.exe from cmd.exe, you should do something like this:

pwsh.exe -noexit -Command "& {ssh test@test}"


Answered By - GreenGrassTunnel
Answer Checked By - Senaida (WPSolving Volunteer)