简体   繁体   English

powershell 到 ssh 中的管道命令不起作用

[英]Piping commands in powershell to ssh not working

I'm trying to pipe commands to a host running a different OS via ssh. I need to send the commands as one string.我正在尝试通过 ssh 向运行不同操作系统的主机发送 pipe 命令。我需要将命令作为一个字符串发送。 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.当我在 ps cli 上测试时,我可以使用引号和换行符让它工作。 For example, sending 3 commands:例如,发送 3 个命令:

>Write-Output "Command1`nCommand2s`nCommand3`n" | ssh -tt user@host > out.txt

The out.txt file gets populated with my command output. out.txt 文件中填充了我的命令 output。

$ Command1
<output omitted>
$ Command2
<output omitted>
$ Command3
<output omitted>

When I try the same thing in ps script it doesn't work:当我在 ps 脚本中尝试同样的事情时,它不起作用:

$cmds="`"Command1``nCommand2``nCommand3``n`""
Write-Output "commands to be sent:" $cmds
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:我得到的 output 显示 $cmds 中的字符串已按照手动 cli 命令正确格式化:

commands to be sent:
"Command1`nCommand2`nCommand3`n"

But on my ssh host it's being interpreted as:但在我的 ssh 主机上,它被解释为:

Error: command 'Command1`nCommand2`nCommand3`n' not recognized

Any idea why?知道为什么吗?

By escaping the $cmds string as you have you are literally sending the " and ` and n characters to the remote system. Did you try it as just:通过 escaping 你所拥有的$cmds字符串实际上是将 " 和 ` 和 n 字符发送到远程系统。你是否尝试过:

$cmds="Command1`nCommand2`nCommand3`n"

This way the output from Write-Output "stuff" and $cmds="stuff"; Write-Output $cmds这样来自Write-Output "stuff"$cmds="stuff"; Write-Output $cmds $cmds="stuff"; Write-Output $cmds would be the same. $cmds="stuff"; Write-Output $cmds是一样的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM