Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Execution of interactive command in remote Powershell session not working

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.

like image 729
gwodus Avatar asked Jan 30 '14 20:01

gwodus


People also ask

What command allows you to enter an interactive PowerShell session with a remote computer?

The Enter-PSSession cmdlet starts an interactive session with a single remote computer. During the session, the commands that you type run on the remote computer, just as if you were typing directly on the remote computer. You can have only one interactive session at a time.

What are the two ways to execute commands in PowerShell on a remote machine?

And the Invoke-Command and New-PSSession commands are two ways to execute PowerShell cmdlets on remote systems.

What PowerShell command would you use to establish a PowerShell session with a remote computer?

The New-PSSession cmdlet creates a PowerShell session (PSSession) on a local or remote computer. When you create a PSSession, PowerShell establishes a persistent connection to the remote computer.


2 Answers

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.

like image 84
x0n Avatar answered Nov 06 '22 22:11

x0n


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.

like image 24
Raf Avatar answered Nov 07 '22 00:11

Raf