profesorfalken / jPowerShell

Simple Java API to interact with PowerShell console
Apache License 2.0
218 stars 84 forks source link

Pipes causes bizarre behavior: command output from wrong command piped in. #54

Open agiles231 opened 6 years ago

agiles231 commented 6 years ago

Suppose you have 4 commands, A, B, C, D and you run: A | B C | D

In some situations, issues will arise where D receives output from A instead of C. I am not sure as to cause and not entirely certain if it is replicable. I worked around this issue by changing the waitPause to a sufficient amount of time, server dependent. If desired, I can work on adding a method specifically for piping output of one command into another. e.g. pShell.executeCommand("Get-ADUser").pipe("Select-Object whenCreated");

pradeepnair87 commented 4 years ago

Even I see a similar issue. What i have noticed is that the previous command has failed( timed out), the next command on the same session returns an output from previous command. jpowershell.execute(cmd1) => ran successfully jpowershell.execute(cmd2) => timed out jpowershell.execute(cmd3) => returns response of cmd1.

pradeepnair87 commented 4 years ago

PowerShellCommandProcessor commandProcessor = new PowerShellCommandProcessor("standard", p.getInputStream(), this.waitPause, this.scriptMode); The inputStream is shared across threads. And in the constructor of PowerShellCommandProcessor, we don't clear the previous read data, if any. So when time out occurs, although the methods returns a timeout error, the data is available in the input stream and the next command reads the already available data and returns almost immediately with the previous data which was not read from the stream. This will result in subsequent calls to return the previous data even with out timeout as the stream already has unread data.