开发者

Execute Powershell command over SSH

Tried following command from Ubuntu host to Windows client over Cygwin/OpenSHH.

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

Output:

Cannot process the command because of a missing parameter. A comm开发者_如何学编程and must follow -Command.

However when i tried the same command over cygwin in windows client i get proper output.

$ 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)


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

When you run this command, the ssh program on the local system gets the arguments:

  • USER@HOSTNAME
  • powershell
  • -Command
  • & {Get-Host}

The double quotes around the last argument are stripped by your local shell before executing ssh. ssh then concatenates the three arguments which make up the remote command into a single string:

  • 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}"'
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜