Executing PowerShell Commands in Java Program -
i have powershell command
need execute using java
program. can guide me how this?
my command get-itemproperty hklm:\software\wow6432node\microsoft\windows\currentversion\uninstall\* | select-object displayname, displayversion, publisher, installdate | format-table –autosize
you should write java program this, here sample based on nirman's tech blog, basic idea execute command calling powershell process this:
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 command"; //getting version string command = "powershell.exe $psversiontable.psversion"; // executing command process powershellprocess = runtime.getruntime().exec(command); // getting 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"); } }
in order execute powershell script
string command = "powershell.exe \"c:\\pathtofile\\script.ps\" ";