我正在尝试使用以下程序使用ProcessBuilder
. 该程序包含本地路径,但我想调用位于网络上的相同脚本。
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class TestPowershell {
public static void main(String[] args) throws IOException
{
Runtime runtime = Runtime.getRuntime();
//Process proc = runtime.exec("cmd.exe /c powershell.exe C:\\get-sevice-gsvc.ps1"); // old code
ProcessBuilder pb = new ProcessBuilder("powershell", "-file", "C:\\get-sevice-gsvc.ps1", "127.0.0.1");
pb.redirectErrorStream(true);
Process proc = pb.start();
InputStream is = proc.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader reader = new BufferedReader(isr);
String line;
try{
while ((line = reader.readLine()) != null)
{
System.out.println(line);
}
reader.close();
proc.getOutputStream().close();
}
catch(Exception e){
System.out.println(e.getMessage());
}
}
}
有什么想法吗?