您是否知道一些用于远程 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/