java - 通过 Java 的远程 Powershell 脚本

标签 java api powershell remote-access

您是否知道一些用于远程 PowerShell 调用的 Java API/库?

从这个 API 我需要这样的东西:

  • 身份验证
  • PowerShell 脚本执行
  • 获取脚本结果

最佳答案

您可以使用 JPowerShell 库:https://github.com/profesorfalken/jPowerShell

PowerShell powerShell = null;
try {
    //Creates PowerShell session
    PowerShell powerShell = PowerShell.openSession();
    //Increase timeout to give enough time to the script to finish
    Map<String, String> config = new HashMap<String, String>();
    config.put("maxWait", "80000");

    //Execute script
    PowerShellResponse response = powerShell.configuration(config).executeScript("./myPath/MyScript.ps1");

    //Print results if the script
    System.out.println("Script output:" + response.getCommandOutput());
} catch(PowerShellNotAvailableException ex) {
    //Handle error when PowerShell is not available in the system
    //Maybe try in another way?
} finally {
    //Always close PowerShell session to free resources.
    if (powerShell != null)
        powerShell.close();
}

关于java - 通过 Java 的远程 Powershell 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36908484/

相关文章:

java - 如何双重转义双引号?

java - 哪些 Eclipse IDE 支持 JSF 2.0?

java - 反射如何影响 Perm 大小?

c# - 未从 .NET MVC MultipartFormDataContent POST 收到 .NET Core API List<IFormFile> 文件

php - 接收和处理无连接错误和200 OK header

windows - PowerShell 找不到项目 - 带空格的路径 IOException

powershell - 打开新的 powershell 控制台并运行命令

powershell:如何以dns格式检索计算机名称

java - 对 1 到 100 之间的所有奇数求和的程序

php - 如何使用vimeo高级API显示视频