Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/387.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Java程序中执行PowerShell命令_Java_Powershell - Fatal编程技术网

在Java程序中执行PowerShell命令

在Java程序中执行PowerShell命令,java,powershell,Java,Powershell,我有一个PowerShell命令,需要使用Java程序执行该命令。有人能指导我怎么做吗 我的命令是Get ItemProperty HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*|选择对象DisplayName、DisplayVersion、Publisher、InstallDate | Format Table–自动调整大小您可以尝试使用以下命令调用powershell.exe: String[]

我有一个
PowerShell命令
,需要使用
Java
程序执行该命令。有人能指导我怎么做吗

我的命令是
Get ItemProperty HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*|选择对象DisplayName、DisplayVersion、Publisher、InstallDate |

Format Table–自动调整大小

您可以尝试使用以下命令调用powershell.exe:

String[] commandList = {"powershell.exe", "-Command", "dir"};  

        ProcessBuilder pb = new ProcessBuilder(commandList);  

        Process p = pb.start();  

您应该编写这样一个java程序,下面是一个基于Nirman技术博客的示例,其基本思想是执行调用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");

 }

}
为了执行powershell脚本

String command = "powershell.exe  \"C:\\Pathtofile\\script.ps\" ";

不需要重新发明轮子。现在你可以用


您可以在命令中使用-ExecutionPolicy RemoteSigned

String cmd=“cmd/c powershell-ExecutionPolicy RemoteSigned-noprofile-noninteractive c:\Users\File.ps1


是的,我已经问过这个问题了,但你能给我一个基本的例子让我开始吗?我想学习这个。你需要练习你的google fu并查一些例子。没有理由你至少不能尝试研究一下。只是从一个旧项目中剪下的代码你通常不需要它。我找到了你的JAR为了不那么健壮,请检查我的问题+答案-->您好,因为我知道您的问题基本上是在您发送给jPowershell的命令字符串中,而不是在JAR中。刚刚看到这篇文章和评论。我尝试了jPowershell,得到了几个简单的示例,所以我只想以轶事的方式支持解决方案。Thanks@profesor_falken如果/a
.ps1
文件的路径包含空格,您可以将
命令
修改为:
String command1=“powershell.exe&'C:/test文件夹和空格/yourPowershellScript.ps1”";
从eclipse运行脚本。如果有人希望一次执行多个命令,请在命令之间使用
-和
-或
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());