Execution of interactive command in remote Powershell session not working

心已入冬 提交于 2019-12-01 17:24:30

问题


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.


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/21466372/execution-of-interactive-command-in-remote-powershell-session-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!