16

I have Windows Server 2003 with CopSSH installed on it ( Cygwin + sshd ). I would like to be able to run a PowerShell script via SSH session command and then use its output. Is there such a capability? How to do it?

AJM
  • 109
  • 5
malloc4k
  • 831
  • 2
  • 9
  • 16

6 Answers6

13

Just invoke powershell with the relevant arguments and pipe it to wherever you want it? You need to make sure it's in the PATH of course.

ak2
  • 562
  • 3
  • 4
  • 1
    Thanks. I did not think it's that simple. – malloc4k May 05 '11 at 13:43
  • 15
    It's not that simple. PowerShell uses the Console API; Cygwin (in the hands of experienced users) is not run in the console, it's run in a terminal emulator. PowerShell doesn't work in a terminal; it hangs because it's trying to use ReadConsoleInput instead of ReadFile on standard input. – Barry Kelly Jun 15 '12 at 10:27
11

Barry Kelly is right.

You need to use my wrapper software that creates a hidden console and runs PowerShell on it.

My page is here: http://sergeybelous.com/SHELL-TERMINAL.html#proxywinconsole.exe

Someone already found my wrapper software and created a tutorial here: https://ssh-with-powershell.blogspot.com/2013/07/enable-ssh-with-powershell-and-remove.html

AJM
  • 109
  • 5
Sergey
  • 111
  • 1
  • 2
  • The URL needs to be updated. Getting 404. – Hans Deragon Apr 19 '21 at 14:12
  • 1
    @HansDeragon I am about to submit an edit updating the URL (I thought it was dead, turns out it just needs an update.) But it'll still be the http version because Firefox flashes up a security warning due to the self-signed certificate on the https version. I know there's nothing to worry about, but some people clicking the link might not look too closely. – AJM Apr 29 '21 at 14:26
9

First thing it is good to add PowerShell's executable path to user's PATH environmental variable. We do it by adding to user's .bashrc file line like:

export PATH=${PATH}:"/cygdrive/c/WINDOWS/system32/WindowsPowerShell/v1.0"

Then we can run PowerShell script just typing in our SSH session

powershell.exe -File "c:\u.ps1"

Of course now we can pipe it to use it's output.

I just wonder why I have to press "Enter" two times in my SSH session after typing the command for it to work.

malloc4k
  • 831
  • 2
  • 9
  • 16
  • 4
    To avoid having to hit return, run the command like: `echo "\n" | powershell.exe ...` – Andrew Jul 23 '11 at 18:07
  • SIngle quotes are also valid - `echo '\n' | powershell.exe ...` - and if you use Git Bash instead of Cygwin, you can press ENTER once without having to pipe in an echoed newline. – AJM Apr 27 '21 at 17:29
  • I've also tried this in msys2 msys, but it failed because their `/usr/bin/cmd` clashed with `cmd.exe`. I've edited the `$PATH` to put the system32 folder at the start, and so far this seems to fix it without any side-effects. – AJM Apr 27 '21 at 21:42
  • @malloc4k Can I ask where you found out that this was possible? I can't find anything else online about invoking PowerShell from a different type of shell and using a "-File" or "/File" parameter to specify a script. – AJM Apr 29 '21 at 14:33
  • Er, never mind. I just tried `powershell -help` and got the information I needed. – AJM Apr 29 '21 at 14:36
4

Try it with no inputformat

powershell.exe -inputformat none -noprofile echo hello

Can be useful for not having to deliver a file to the local machine.

3

If you need to run powershell inside cygwin/babun, follow https://code.google.com/p/mintty/issues/detail?id=56#c64 . Bascilly, downloard or compile https://github.com/rprichard/winpty, copy it to your $PATH and then run

console.exe powershell

This also works with batch scripts that invoke powershell inside.

Rolf
  • 131
  • 1
0
Administrator@AAAAA-11111:~ $cd /cygdrive/c/WINDOWS/system32/WindowsPowerShell/v1.0
Administrator@AAAAA-11111:/cygdrive/c/WINDOWS/system32/WindowsPowerShell/v1.0 $ cmd /k powershell.exe
Windows PowerShell
Copyright (C) 2016 Microsoft Corporation. All rights reserved.

PS C:\WINDOWS\system32\WindowsPowerShell\v1.0>

this works for me.

explorer
  • 111
  • 1