Piping commands in powershell to ssh not working

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 3 years and 1 month old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
generalchang
Posts: 1
Last visit: Wed Jan 27, 2021 12:35 pm

Piping commands in powershell to ssh not working

Post by generalchang »

I'm trying to pipe commands to a host running a different OS via ssh. I need to send the commands as one string. Sending one at a time isn't an option. I can get this to work using quotes and newlines when I test on the ps cli. For example, sending 3 commands:
  1. Write-Output "Command1`nCommand2s`nCommand3`n" | ssh -tt user@host > out.txt
The out.txt file gets populated with my command output.
  1. $ Command1
  2. <output omitted>
  3. $ Command2
  4. <output omitted>
  5. $ Command3
  6. <output omitted>
When I try the same thing in ps script it doesn't work:
  1. $cmds="`"Command1``nCommand2``nCommand3``n`""
  2. Write-Output "commands to be sent:" $cmds
  3. Write-Output $cmds | ssh -tt user@host > out.txt
The output I get shows that the string in $cmds is being formatted correctly as per the manual cli command:
  1. commands to be sent:
  2. "Command1`nCommand2`nCommand3`n"
But on my ssh host it's being interpreted as:
  1. Error: command 'Command1`nCommand2`nCommand3`n' not recognized
Any idea why?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Piping commands in powershell to ssh not working

Post by jvierra »

Too many baqtics. Only one per meta character required. Two escapes the character.
This topic is 3 years and 1 month old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked