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.
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.
And the Invoke-Command and New-PSSession commands are two ways to execute PowerShell cmdlets on remote systems.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With