Click here to Skip to main content
15,888,065 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a java code that execute a power shell script
my parameters are in a string array. how should I put the parameters to the script every time i execute the code?



Java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class PowerShellCommand {

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

  //String command = "powershell.exe  your command";
  //Getting the version
  String command = "powershell.exe  my powershell script ";
  // Executing the command
  Process powerShellProcess = Runtime.getRuntime().exec(command);
  // Getting the results
  powerShellProcess.getOutputStream().close();
  String line;
  System.out.println("Standard Output:");
  BufferedReader stdout = new BufferedReader(new InputStreamReader(
    powerShellProcess.getInputStream()));
  while ((line = stdout.readLine()) != null) {
   System.out.println(line);
  }
  stdout.close();
  System.out.println("Standard Error:");
  BufferedReader stderr = new BufferedReader(new InputStreamReader(
    powerShellProcess.getErrorStream()));
  while ((line = stderr.readLine()) != null) {
   System.out.println(line);
  }
  stderr.close();
  System.out.println("Done");

 }

}


What I have tried:

i didn't find anything helpful on google
Posted
Updated 20-Nov-16 6:49am
v2
Comments
[no name] 20-Nov-16 10:17am    
"didn't find anything helpful on google", you couldn't find anything on google about iterating over a string array in java? You must not have tried very hard.
Atiiiii 20-Nov-16 10:29am    
if you couldn't help just don't answer the question!no need to make fun of me!!! I want to set my power shell parameters from an array,I don't know how it works.
[no name] 20-Nov-16 13:16pm    
A simple google search would have told you all you need to know. You know that you didn't try very hard so it can't be making fun of you. Anyone reading your not-a-question can see you tried nothing at all.

1 solution

I would suggest setting up the power shell command in a command window first so you can see how the parameters need to be setup. Once you have that working you can modify your Java code accordingly.
 
Share this answer
 
Comments
Atiiiii 20-Nov-16 14:37pm    
I set up the power shell command,it worked correctly,i gave some random parameters to it at my java code,and it worked correctly, now i want to send my parameters from an array in my program for the power shell command in that code and set them,I don't know how to do this?
could you help me?
Richard MacCutchan 21-Nov-16 4:14am    
Take a look at Runtime (Java Platform SE 7 )[^] for how to set the variable parameters.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900