7

I am having troubles executing commands in a remote PowerShell session which need user interaction.

Example: I enter a remote session

Enter-PSSession -ComputerName mobius

In this session I execute a command which asks for a password:

[mobius]: PS C:\Windows\system32> & 'c:\Program Files (x86)\Putty\plink.exe' merlin -l joe

joe@merlin's password:
c:\Program Files (x86)\Putty\plink.exe : Using username "plakat".
    + CategoryInfo          : NotSpecified: (Using username "plakat".:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError

The last two lines are displayed in red. There seem to be two problems.

Problem 1: plink.exe writes the text 'Using username "plakat"' to stderr. This probably causes the error message. Can I suppress this somehow? (pipe stderr to stdout or something.)

Problem 2: The process exits at the point where I should enter the password. I can also reproduce that with other commands like

[mobius]: PS C:\Windows\system32> cmd /c date

It does not let me enter a date. Both commands work if I run them in a local PowerShell. Neither Problem 1 or 2 are showing in that case.

2 Answers 2

3

Interactive native windows console commands are not supported in a remote powershell session. I know this sounds dumb, but currently this is the case (as of PowerShell v4.0).

Most command line utilities support some form of automation, be it piping or passing values as arguments so take a closer look at the tools you're using. Of course this is on a case by case basis. There is no easy way to intercept an interactive prompt on the remote end in any universal manner.

Sign up to request clarification or add additional context in comments.

8 Comments

Thanks. Hmm. I was afraid of that. In my example I can use plink with public-key authentication to circumvent the password prompt. But that only shifts my problem. Because my final goal is to open an interactive ssh session to a third machine with plink (which is a command line ssh client). I find it somehow strange that this is not possible. What if I would like to edit a file on the remote server or use an interactive command line debugger?
Editing text files on remote servers is a distinctly unix-like activity for administrators. In the Windows world, APIs are king, and are usually remotable (wmi, dcom, winrm/soap). Any debugger worth its salt can connect to a remote server. Command line debuggers at that are rare: I only know of two, one of which is never used remotely (outside of a serial connection.) -- CDB and MDBG.
So, just to add to that wall of text, if you want remote interactivity then install an SSH server.
Since it does not seem to be possible, I accept this as answer. I did in fact install a SSH server. It's funny though, that MS went so far, but did not go the whole length. Why have a command like Enter-PSSession which opens an interactive remote session, but does not allow much interactivity? Hopefully in future versions they will implement it...
@Turtle1363 Yeah -- the psremoting protocol is not capable of managing a remote tty with ssh. You might have some luck with ssh -t [email protected] "command" to force a line-oriented return, i.e. run one command at a time
|
1

You can sidestep the issue by using Invoke-command cmdlet which executes and exits

Invoke-Command -ComputerName Mobius -ScriptBlock {## your stuff}

and combine it with pipe so in the case of the date command

Invoke-Command -ComputerName Mobius -ScriptBlock {echo "01-02-1999" |cmd /c date}

or for plink

Invoke-Command -ComputerName Mobius -ScriptBlock {"yourpassword"| &'c:\Program Files (x86)\Putty\plink.exe' merlin -l joe `"bashcommand1 && bashcommand2`" }

or even simpler

Invoke-Command -ComputerName Mobius -ScriptBlock { &'c:\Program Files (x86)\Putty\plink.exe' merlin -l joe -pw yourpass `"bashcommand1 && bashcommand2`" }

but as @X0n said no real interactivity.

1 Comment

Like I said in a comment to X0n's answer. The password was not really my problem. I still needed real interactivity further on. But thanks for the tip. I might need some piping like you did for some smaller scripted remote tasks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.