-1

I am trying to run a powershell command in eclipse with the following code. The powershell script display the list of installed application on windows. The script works fine when it is executed in powershell. But I am unable to get the output on the console. Could someone please tell me what is the problem here?

import com.profesorfalken.jpowershell.PowerShell;
import com.profesorfalken.jpowershell.PowerShellNotAvailableException;
import com.profesorfalken.jpowershell.PowerShellResponse;

public class TestProcessList {

public static void main(String[] args) throws Exception {

    try {
        PowerShell powerShell = PowerShell.openSession();
        String command = "Get-ItemProperty " + 
                "HKLM:/Software/Wow6432Node/Microsoft/Windows/CurrentVersion/Uninstall/* " + 
                "| Select-Object DisplayName, DisplayVersion, Publisher, InstallDate " + 
                "| Format-Table –AutoSize";

        PowerShellResponse response = powerShell.executeCommand(command);

        System.out.println("Proceses are:" + response.getCommandOutput());

        powerShell.close();

    } catch (PowerShellNotAvailableException ex) {
        throw new RuntimeException("Failed to run PowerShell", ex);
    }

}

}

ranjitha rv
  • 11
  • 1
  • 5

3 Answers3

1

Probably there are thrown some exceptions. Which in your case are not being re-thrown and are consumed (Bad practice). Change tthe catch block to:

} catch (PowerShellNotAvailableException ex) {
    throw new RuntimeException("Failed to run PowerShell", ex)
}

Then you will see what went wrong including its whole stacktrace and possible causes.

UPDATE: You are actually using piped commands ("|" in execute command string) inside of single command. It wont't work as pipes are not easy to implement in java. Try out solution basing on following example for command "ps aux | grep java":

import org.apache.commons.io.IOUtils;

public static void main(String[] args) throws Exception
{
    Process p1 = Runtime.getRuntime().exec(new String[] { "ps", "aux" });
    InputStream input = p1.getInputStream();
    Process p2 = Runtime.getRuntime().exec(new String[] { "grep", "java" });
    OutputStream output = p2.getOutputStream();
    IOUtils.copy(input, output);
    output.close(); // signals grep to finish
    List<String> result = IOUtils.readLines(p2.getInputStream());
    System.out.println(result);
}

Source: https://stackoverflow.com/a/7226858/1688570

As API of the PowerShell library is not known to me, you have to adapt the example to work with PowerShell library by yourself.

Community
  • 1
  • 1
TouDick
  • 1,262
  • 12
  • 18
1

Code From PowerShell.java class.

int closingTime = 0;

while (!closeTask.isDone () 
    && !closeTask.isDone()) {
            if (closingTime > MAX_WAIT) {
        Logger.getLogger(PowerShell.class.getName()).log(Level.SEVERE, "Unexpected error when closing PowerShell: TIMEOUT!");
        break;
    }
    Thread.sleep(WAIT_PAUSE);
    closingTime += WAIT_PAUSE;

and

static final int WAIT_PAUSE = 10;
static final int MAX_WAIT = 2000;

This means your command takes more than 2000 milliseconds to close/complete. I think adding a custom sleep in your code might help. I am not familiar with JPowerShell , you should take a look at PowerShell class. or try with TouDick's answer.

Arun Xavier
  • 763
  • 8
  • 47
0

you have to add some wait time. I was executing some commands using java and was not able to print command output on console even though I was using response.getCommandOutput(). So, I tried below by adding wait:

PowerShellResponse response;
Map<String, String> maxWait = new HashMap<String, String>();
maxWait.put("maxWait", "300000");
PowerShell powerShell = PowerShell.openSession().configuration(maxWait);
response = powerShell.executeCommand(yourCommand);
System.out.println(response.getCommandOutput());

The wait time is to wait for maximum time.