0

I am developing a script which uses ssh to connect to a windows host, run a powershell command, and parse the output. While I can connect to the host and run the command, powershell will not exit and return control back to the local script until I press the enter key.

At the moment, the specific command being run is ssh HOSTNAME 'echo $(hostname)' and the ssh server is configured to pass remote execution requests to powershell -noninteractive -command CMD, where HOSTNAME is the name of the windows host and CMD is the remote command to be run (in this case echo $(hostname)).

The end goal is to have the script which is calling remote powershell commands to run completely noninteractively, but this is currently impossible as the powershell command will not run noninteractively.

How do I get powershell to run remote commands noninteractively?

mfabel
  • 178
  • 1
  • 12
  • Are you seeing the correct response? Note that you aren't supposed to put quotes around your command to powershell, the only thing after the `-command` should be what you want to run. – Guvante Feb 26 '15 at 22:59
  • @Guvante Yes, I am seeing the correct response. I removed the quotes, and that hasn't changed it's behavior. – mfabel Feb 26 '15 at 23:16
  • I tried running your sample command locally and it prints and exits immediately. Are you sure your method of calling powershell isn't causing the problem? – Guvante Feb 27 '15 at 00:20
  • @Guvante No, I'm not sure. I'm calling powershell over ssh with the flags mentioned in the question, which seem to be the right ones from reading manual pages and such. I would be inclined to think the issue is in the powershell flags, but I haven't been able to prove or disprove that. – mfabel Feb 27 '15 at 00:27
  • 1
    I am also not able to reproduce the issue locally, but perhaps a workaround would be acceptable. Try putting this at the end of your command `;kill $PID` – Booga Roo Feb 27 '15 at 01:18

1 Answers1

0

I've resolved the issue. While I haven't determined what was causing the issue, I can at least overcome the issue at hand by redirecting stdin to /dev/null on the side that initiates the ssh connection.

$ ssh HOSTNAME "CMD" </dev/null

This solution doesn't involve powershell at all, but rather treats the symptoms from the other side of the connection.

mfabel
  • 178
  • 1
  • 12