0

I am trying to invoke PowerShell cmdlets and scripts from a Java Application. I am using jPowerShell . It works as expected as expected with PowerShell version 6.0.x. However, when I am running PowerShell 7.x, the executeCommand() method times out. Just wanted to find out if anyone else have had this issue and how you resolved it. I'd appreciate if you can let me know. Thanks

Here is my sample code:

   ..
   ..
   //Creates PowerShell session (we can execute several commands in the same session)
   try (PowerShell powerShell = PowerShell.openSession()) {
       //Execute a command in PowerShell session
       PowerShell powerShell = PowerShell.openSession("pwsh"); // pwsh is the executable in Linux
       Map<String, String> config = new HashMap<String, String>();
       config.put("maxWait", "60000"); // wait for a minute
       PowerShellResponse response = powerShell.executeCommand("Get-Process");

       responseBody = response.getCommandOutput();
       responseCode = response.isError() ? 1 : 0;
   } catch(Exception e) {
       StringBuffer sb = new StringBuffer(e.getMessage());
       return toResponse(responseCode, sb.toString());
   }
   ..
   ..

Please note that I am running the above inside a Docker container and I am able to see the "pwsh -nologo -noexit -Command -" process running in the container before it times out.

  • Jay

I tried to run a PowerShell cmdlet from Java code using jPowerShell and I was expecting the result of the PowerShell cmdlet (for example, Get-Date, Get-Process, etc)

0 Answers0