简体   繁体   English

在远程Powershell会话中执行交互式命令不起作用

[英]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. 我在需要用户交互的远程PowerShell会话中执行命令时遇到麻烦。

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. 问题1: plink.exe将文本“使用用户名“ plakat””写入stderr。 This probably causes the error message. 这可能会导致错误消息。 Can I suppress this somehow? 我能以某种方式抑制这种情况吗? (pipe stderr to stdout or something.) (将stderr传递到stdout或其他东西。)

Problem 2: The process exits at the point where I should enter the password. 问题2:该过程在我应该输入密码的位置退出。 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. 如果我在本地PowerShell中运行它们,这两个命令都可以工作。 Neither Problem 1 or 2 are showing in that case. 在这种情况下,问题1或2均未显示。

Interactive native windows console commands are not supported in a remote powershell session. 远程Powershell会话中不支持交互式本机Windows控制台命令。 I know this sounds dumb, but currently this is the case (as of PowerShell v4.0). 我知道这听起来很愚蠢,但目前是这样(从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. 没有任何简单的方法可以以任何通用方式在远端拦截交互式提示。

You can sidestep the issue by using Invoke-command cmdlet which executes and exits 您可以使用执行并退出的Invoke-command cmdlet来回避问题

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

and combine it with pipe so in the case of the date command 并将其与管道结合起来,因此在date命令的情况下

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

or for plink 或用于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. 但是正如@ X0n所说的,没有真正的交互性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM