Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Command execution times out in PowerShell 7.x.x but works in 6.x.x #82

Open
jayatdizzion opened this issue Jan 10, 2023 · 1 comment
Open

Comments

@jayatdizzion
Copy link

jayatdizzion commented Jan 10, 2023

Hi there,
Commands like Get-Date, Get-Process, etc times out when executed using JPowerShell in PowerShell 7.x.x. The same work fine in version 6.x.x. Also this happens when PowerShell is running in a Docker container. Here is a sample program to reproduce this:

**import com.profesorfalken.jpowershell.PowerShell;
import com.profesorfalken.jpowershell.PowerShellResponse;
import java.util.HashMap;
import java.util.Map;

public class psTester {
public static void main (String[] args) {
testPsCmd();
}

private static void testPsCmd() {
String responseBody = "";
int responseCode = -1;
try {
PowerShell powerShell = PowerShell.openSession("pwsh");
Map<String, String> config = new HashMap<String, String>();
config.put("maxWait", "6000");
PowerShellResponse response =
powerShell
.configuration(config)
.executeCommand("Get-Process");
responseCode = response.isError() ? 1 : 0;
responseBody = response.getCommandOutput();
powerShell.close();
} catch (Exception e) {
StringBuffer sb = new StringBuffer(e.getMessage());
}
}
}**

Here is a Dockerfile that can be used to spin up a container with JDK11 and PowerShell 7.2.7:
**FROM amazoncorretto:11 as corretto_jdk

RUN yum install -y sudo
RUN sudo yum install -y wget
RUN sudo yum install -y tar
RUN sudo yum install -y gzip
RUN sudo yum install -y curl libunwind libicu libcurl openssl libuuid.x86_64

RUN curl -L -o /tmp/powershell.tar.gz https://github.com/PowerShell/PowerShell/releases/download/v7.2.7/powershell-7.2.7-linux-x64.tar.gz
RUN sudo mkdir -p /opt/microsoft/powershell/7.2.7
RUN sudo tar zxf /tmp/powershell.tar.gz -C /opt/microsoft/powershell/7.2.7
RUN sudo chmod +x /opt/microsoft/powershell/7.2.7/pwsh
RUN sudo ln -s /opt/microsoft/powershell/7.2.7/pwsh /usr/bin/pwsh
RUN sudo ln -s /opt/microsoft/powershell/7.2.7/pwsh /usr/bin/powershell**

Please let me know if you need more information. Thanks

@jenskreidler
Copy link

jenskreidler commented Sep 7, 2023

Regarding the source code above, you're using Powershell/Unix. Despite having pwsh run <7.x, I am running as well into this issue.
Tracing down the execution of jPowerShell on pwsh 7.x, jPowerShell hangs in PowerShellCommandProcessor at readLine(). I yet don't know why, but there are far more than one resource in the internet about "BufferedReader hanging...". What I know is, pwsh echoes some control characters too every time an output is read from the Process's outputstream jPowerShell captures.

I tracked it down to "CSI ? 1 h" and "CSI ? 1l" - special ANSI control sequences for the key cursor mode (https://vt100.net/docs/vt510-rm/chapter4.html - Table 4-8). I also attached a log image showing the pwsh output
Steuerzeichen in pwsh output.

In my fork I provisionally changed that readLine to a character-wise read() implementation (no so efficient) - but jPowerShell is capable to run without hanging/blocking. On unix this also applies to the "getPID()", "isLastCommandInError()" and "exit" calls in PowerShell.java
Atop of this, I strip undesired characters from the command output (to get rid of these control characters) - in the application using jPowerShell.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants