Closed xychen7 closed 5 years ago
nvm, I turn to processBuilder and it works perfectly. In case anyone face same issue, below is my code :
try {
String powershell = "C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe";
String scripts = System.getProperty("user.dir")+"\\scripts\\test.ps1";
String msg= "";
ProcessBuilder builder = new ProcessBuilder( powershell, "\""+scripts+" "+param+"\"");
builder.redirectErrorStream(true);
Process p = builder.start();
BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
while (true) {
line = r.readLine();
if (line == null) break;
msg = msg+line.trim();
}
r.close();
if (p.exitValue() != 0 )
System.out.print(msg);
else System.out.print("Successfully execute script.");
}catch (Exception e) {
System.out.print(e.getMessage());
}
Hi, I'm trying to execute a powershell script to run psql to create database. I purposely give a invalid IP address to get the error message but getCommandOutput() return nothing although isLastCommandInError () return True . How can I get the actual output of the script ?
My code :
My powershell script :
The error message if I execute the script in powershell :
Any help appreciated.