在 Java 程序中执行 PowerShell 命令
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29545611/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Executing PowerShell Commands in Java Program
提问by question
I have a PowerShell Command
which I need to execute using Java
program. Can somebody guide me how to do this?
我有一个PowerShell Command
我需要使用Java
程序执行的。有人可以指导我如何做到这一点吗?
My command is Get-ItemProperty HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\* | Select-Object DisplayName, DisplayVersion, Publisher, InstallDate |
Format-Table –AutoSize
我的命令是 Get-ItemProperty HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\* | Select-Object DisplayName, DisplayVersion, Publisher, InstallDate |
Format-Table –AutoSize
采纳答案by anquegi
You should write a java program like this, here is a sample based on Nirman's Tech Blog, the basic idea is to execute the command calling the PowerShell process like this:
你应该像这样写一个java程序,这里是一个基于Nirman's Tech Blog的示例,基本思想是执行调用PowerShell进程的命令是这样的:
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 $PSVersionTable.PSVersion";
// 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");
}
}
In order to execute a powershell script
为了执行powershell脚本
String command = "powershell.exe \"C:\Pathtofile\script.ps\" ";
回答by Katarak
you can try to calle the powershell.exe with some commands like :
您可以尝试使用以下命令调用 powershell.exe:
String[] commandList = {"powershell.exe", "-Command", "dir"};
ProcessBuilder pb = new ProcessBuilder(commandList);
Process p = pb.start();
回答by profesor_falken
No need of reinvent the wheel. Now you can just use jPowerShell
无需重新发明轮子。现在你可以使用jPowerShell
String command = "Get-ItemProperty " +
"HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\* " +
"| Select-Object DisplayName, DisplayVersion, Publisher, InstallDate " +
"| Format-Table –AutoSize";
System.out.println(PowerShell.executeSingleCommand(command).getCommandOutput());
回答by Nitin Kumar
You can use -ExecutionPolicy RemoteSigned in the command.
您可以在命令中使用 -ExecutionPolicy RemoteSigned。
String cmd = "cmd /c powershell -ExecutionPolicy RemoteSigned -noprofile -noninteractive C:\Users\File.ps1
String cmd = "cmd /c powershell -ExecutionPolicy RemoteSigned -noprofile -noninteractive C:\Users\File.ps1