【问题标题】:Executing PowerShell Commands in Java Program在 Java 程序中执行 PowerShell 命令
【发布时间】:2015-06-15 05:43:44
【问题描述】:

我有一个PowerShell Command,我需要使用Java 程序来执行它。有人可以指导我如何做到这一点吗?

我的命令是Get-ItemProperty HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\* | Select-Object DisplayName, DisplayVersion, Publisher, InstallDate | Format-Table –AutoSize

【问题讨论】:

  • 是的,我已经回答了这个问题,但是你能给我一个基本的例子来帮助我吗?我想学这个。
  • 你需要锻炼你的 google-fu 并查找一些示例。您没有理由至少不能尝试对此进行研究。

标签: java powershell


【解决方案1】:

你应该这样写一个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\" ";

【讨论】:

  • 如果/a .ps1 文件的路径包含空格,可以将command 修改为:String command1 = "powershell.exe & 'C:/test folder with spaces/yourPowershellScript.ps1' "; 以从eclipse运行脚本。
  • 如果有人想一次执行多个命令,请在命令之间使用-and-or 来连接它们。
【解决方案2】:

无需重新发明轮子。现在你可以使用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());

【讨论】:

  • 我发现你的 JAR 不是那么健壮,检查我的问题 + 答案 ---> stackoverflow.com/questions/42065666/…
  • 您好,据我了解,您的问题基本上出在您发送到 jPowershell 的命令字符串中,而不是在 JAR 本身中。
  • 刚刚看到这篇文章并发表了评论。我尝试了 jPowerShell 并得到了几个简单的示例,所以只是想给我的解决方案的轶事支持。谢谢@profesor_falken
【解决方案3】:

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

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

        ProcessBuilder pb = new ProcessBuilder(commandList);  

        Process p = pb.start();  

【讨论】:

  • 只是从你通常不需要的旧项目中截取的代码
【解决方案4】:

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

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

【讨论】:

    猜你喜欢
    • 2011-01-16
    • 2017-05-03
    • 1970-01-01
    • 1970-01-01
    • 2021-05-10
    • 2012-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多