1

I want to run a powershell in some remote machine using java. How can this be achieved.? currently this code is not running the powershell command locally.!!

  public void main() throws Exception{

  String[] command = { /*"cmd.exe", "/C",*/ "powershell", "Command","&","echo '********** hello world ********'"};
  String out;

  ProcessBuilder processBuilder = new ProcessBuilder(command);

  Process process = null;

  try {
                            process = processBuilder.start();
                             Logger.getInstance().info("process started" );

                        } catch (IOException e2) {
                            // TODO Auto-generated catch block
                            e2.printStackTrace();
                        }


                        java.io.InputStream is = process.getInputStream();
                        InputStreamReader isr = new InputStreamReader(is);

                        BufferedReader br = new BufferedReader(isr);
                        // create a reader for the return data from cmd.
                        StringBuilder sb = new StringBuilder();
                        // create a string builder to automate the string addition

                        try {
                            while ((out = br.readLine()) != null) {// build the input
                                                                        // string from
                                                                        // cmd.

                                sb = sb.append(out);
                    System.out.println(out);

                            }


                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }

this code runs for indefinite time. without any output.

1 Answer 1

3

You can try to run something like "invoke-command -computername Server1 -filepath c:\scripts\script.ps1"

    Process p = new ProcessBuilder()
            .inheritIO()
            .command("invoke-command", "-computername", "Server1",
                    "-filepath", "C:\\scripts\\script.ps1").start();
    p.waitFor();

but first of all make sure that this command work OK from command line.

Sign up to request clarification or add additional context in comments.

6 Comments

Can you tell me how to do this via java method.?
Currently I am juss trying to run a powershell command locally
Please see my edit and let me know what went wrong in the code. which will run a powershell command locally.
new ProcessBuilder().inheritIO().command("powershell", "echo", "111").start(); works OK for me and prints 111,try it
cannot resolve importIO(), which import did you use. I have used import.java.io.*
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.