您可能知道一些用于Java的API来调用远程PowerShell脚本吗?
通过此API,我需要执行以下操作:
还是存在另一种方法?
谢谢
最佳答案
您可以使用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();
}