简体   繁体   English

通过SSH执行Powershell命令

[英]Execute Powershell command over SSH

Tried following command from Ubuntu host to Windows client over Cygwin/OpenSHH. 在Ubuntu主机上通过Cygwin / OpenSHH从Windows客户端尝试了以下命令。

ssh  USER@HOSTNAME  powershell -Command "& {Get-Host}"

Output: 输出:

Cannot process the command because of a missing parameter. A command must follow -Command.

However when i tried the same command over cygwin in windows client i get proper output. 但是,当我在Windows客户端中通过cygwin尝试相同的命令时,会得到正确的输出。

$ powershell -Command "& {Get-Host}"
Welcome to Powershell augmented with NaviNet tools.
Bootstrapped ScmToolsProjectConfiguration.


Name             : ConsoleHost
Version          : 2.0
InstanceId       : 0b2e578e-50b9-42ab-a07c-0c4ef762ba0c
UI               : System.Management.Automation.Internal.Host.InternalHostUser
CurrentCulture   : en-US
CurrentUICulture : en-US
PrivateData      : Microsoft.PowerShell.ConsoleHost+ConsoleColorProxy
IsRunspacePushed : False
Runspace         : System.Management.Automation.Runspaces.LocalRunspace

Try to escape the ", so it looks like: 尝试转义“”,使其看起来像:

ssh USER@HOSTNAME powershell -Command \"& {Get-Host}\"

If this doesn't work, try to escape the {,} and/or & too. 如果这不起作用,请尝试转义{,}和/或&。 I think it's a escaping problem ;) 我认为这是一个无法解决的问题;)

EDIT: 编辑:

Or even try to escape it with \\\\\\", which results in an escaped " and a \\. 甚至尝试用\\\\\\“对其进行转义,这会导致转义的”和\\。 (dunno how powershell wants the command, not familiar with it) (不知道PowerShell是如何想要该命令的,对此并不熟悉)

ssh USER@HOSTNAME powershell -Command "& {Get-Host}"

When you run this command, the ssh program on the local system gets the arguments: 运行此命令时,本地系统上的ssh程序将获取参数:

  • USER@HOSTNAME USER @ HOSTNAME
  • powershell 电源外壳
  • -Command -命令
  • & {Get-Host} 和{获取主机}

The double quotes around the last argument are stripped by your local shell before executing ssh . 在执行ssh之前,最后一个参数周围的双引号会被您的本地shell删除。 ssh then concatenates the three arguments which make up the remote command into a single string: ssh然后将组成远程命令的三个参数串联为一个字符串:

  • powershell -Command & {Get-Host} powershell -Command和{Get-Host}

It sends this string to the remote system to be executed. 它将此字符串发送到要执行的远程系统。 The remote system isn't interpreting this string the way you want it to. 远程系统未按照您希望的方式解释此字符串。

You need to issue the command in such a way that the quotes are included in the version of the command that's sent to the remote system. 您需要以这样一种方式发出命令,即引号包含在发送到远程系统的命令版本中。 This should do it: 应该这样做:

ssh USER@HOSTNAME 'powershell -Command "& {Get-Host}"'

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

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