0

I have a java code that execute a powershell script.My parameters are in a string array that I got from user.

String sentence = clientinp.readUTF();
            String[] parts = sentence.split(",");

How should I put the parameters to the script every time I execute the code? I tried this code:

String command = "powershell.exe  $Add-DnsServerResourceRecordA -ZoneName -Name -IPv4Address -TimeToLive";

But I don't know how can I pass this array to powershell.What should I do?

8
  • If you have made sure that the input parameters are in the exact order you need them you can simply ask for the elements to do stuff like "powershell.exe $Add-DnsServerResourceRecordA" + parts[0] +" ... " + parts[...]; Commented Nov 21, 2016 at 11:11
  • [link] stackoverflow.com/questions/29545611/… <- this will help you Commented Nov 21, 2016 at 11:17
  • @px06 yes I'm sure.i should try this: "powershell.exe $Add-DnsServerResourceRecordA + parts[0] + parts[1]" ; without " end of $Add-DnsServerResourceRecordA" ? Commented Nov 21, 2016 at 11:20
  • @px06 I wrote ("powershell.exe $Add-DnsServerResourceRecordA + parts[0] + parts[1]") or ("powershell.exe $Add-DnsServerResourceRecordA + 'parts[0]' + 'parts[1]' " ) but i have ioexeption Commented Nov 21, 2016 at 11:27
  • @Onkar I used this.now when i execute the powershell I want to pass my array instead of enter the value myself : String command = "powershell.exe $Add-DnsServerResourceRecordA -ZoneName -Name -IPv4Address -TimeToLive"; Commented Nov 21, 2016 at 11:34

1 Answer 1

0

Use a ProcessBuilder. You have to put each parameter (including path to the program) as items to an array or list and pass it to the constructor of ProcessBuilder.

for example:

String[] arguments = {"powershell.exe", "$Add-DnsServerResourceRecordA", "-ZoneName", "[your zone name]", "-Name", "[your name]", "-IPv4Address", "[your ipv4 address]", "-TimeToLive", "[your TTL]"};
ProcessBuilder processBuilder = new ProcessBuilder(arguments);
Process process = processBuilder.start();

As an alternative you can use Runtime.getRuntime().exec()

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

Comments

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.