Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remote Powershell scripts via Java

Are you aware of some Java APIs/Libraries for remote PowerShell invocation?

From this API I need things like:

  • Authentication
  • PowerShell Scripts execution
  • Getting results of scripts
like image 394
Vladlen Gladis Avatar asked Apr 28 '16 07:04

Vladlen Gladis


1 Answers

You can use JPowerShell library: 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();
}
like image 99
digz6666 Avatar answered Sep 21 '22 14:09

digz6666