Click here to Skip to main content
15,893,486 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a java code that execute a powershell script.My parameters are in a string array that I got from user.
Java
String sentence = clientinp.readUTF();
                 String[] parts = sentence.split(",");


What I have tried:

How should I put the parameters to the script every time I execute the code? I tried this code:
Java
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?
Posted
Updated 21-Nov-16 4:57am

Use the Formatter (Java Platform SE 7 )[^] to build a string with parameters.

Example:
Java
String command = String.format("powershell.exe  $Add-DnsServerResourceRecordA -ZoneName %1$s -Name %2$s -IPv4Address %3$s -TimeToLive %4$s", parts[0], parts[1], parts[2], parts[3]);

Note that the above may be not what you want because you did not specify what the input is and if it matches the parameters expected by the Add-DnsServerResourceRecordA cmdlet.
 
Share this answer
 
Comments
Atiiiii 21-Nov-16 10:10am    
I really appreciate for your help, it worked perfectly without the first $ before Add-DnsServerResourceRecordA
Jochen Arndt 21-Nov-16 10:40am    
I copied and pasted it from your post but should have noticed that it must be removed.

Thank you for your feedback and accepting the solution.
Atiiiii 21-Nov-16 10:54am    
yes I didn't notice it at first either,thank you very much :)
could I be in contact with you?
Jochen Arndt 21-Nov-16 11:32am    
If you need further help you should just post new questions. You will probably get faster and better help then because the questions will be seen by more people.
Especially for Java because I know it only a little bit.
Atiiiii 21-Nov-16 11:45am    
ok,thanks for your guidance
I gave you a link to the documentation this morning: How to set powershell script parameters[^] . Did you try it?
 
Share this answer
 
Comments
Atiiiii 21-Nov-16 11:15am    
I couldn't open your link because it has been filterd for me (I'm Iranian & I don't have access to it)
But Thanks for your help :)

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