Giter Club home page Giter Club logo

jpowershell's Introduction

jPowerShell

Simple Java API that allows to interact with PowerShell console

    PowerShell.openSession()
              .executeCommandAndChain("Get-Process", (res -> System.out.println("List Processes:" + res.getCommandOutput())))
              .executeCommandAndChain("Get-WmiObject Win32_BIOS", (res -> System.out.println("BIOS information:" + res.getCommandOutput())))
              .close();

New JPowerShell v3 is out

The version 3 of JPowerShell includes an important revision and rewrite of most of the code that improves performance and stability.

Check all the new features here: https://github.com/profesorfalken/jPowerShell/wiki/New-3.0-version-of-PowerShell

Installation

To install jPowerShell you can add the dependecy to your software project management tool: https://search.maven.org/artifact/com.profesorfalken/jPowerShell/3.1.1/jar

For example, for Maven you have just to add to your pom.xml:

  <dependency>
    <groupId>com.profesorfalken</groupId>
    <artifactId>jPowerShell</artifactId>
    <version>3.1.1</version>
  </dependency>

Instead, you can direct download the JAR file and add it to your classpath. https://repo1.maven.org/maven2/com/profesorfalken/jPowerShell/3.1.1/jPowerShell-3.1.1.jar

Basic Usage

Single command execution

If you only need to execute a single command, this is the quickest way to do it.

   //Execute a command in PowerShell session
   PowerShellResponse response = PowerShell.executeSingleCommand("Get-Process");

   //Print results
   System.out.println("List Processes:" + response.getCommandOutput());

Executing one or multiple commands using the same PowerShell session

If you have to execute multiple commands, it is recommended to reuse the same session in order to be more efficient (each session has to open a PowerShell console process in the background).

   //Creates PowerShell session (we can execute several commands in the same session)
   try (PowerShell powerShell = PowerShell.openSession()) {
       //Execute a command in PowerShell session
       PowerShellResponse response = powerShell.executeCommand("Get-Process");

       //Print results
       System.out.println("List Processes:" + response.getCommandOutput());

       //Execute another command in the same PowerShell session
       response = powerShell.executeCommand("Get-WmiObject Win32_BIOS");

       //Print results
       System.out.println("BIOS information:" + response.getCommandOutput());
   } catch(PowerShellNotAvailableException ex) {
       //Handle error when PowerShell is not available in the system
       //Maybe try in another way?
   }

You can also choose to execute the same commands with a more fluent style using the executeCommandAndChain method:

    PowerShell.openSession()
                    .executeCommandAndChain("Get-Process", (res -> System.out.println("List Processes:" + res.getCommandOutput())))
                    .executeCommandAndChain("Get-WmiObject Win32_BIOS", (res -> System.out.println("BIOS information:" + res.getCommandOutput())))
                    .close();

Configure jPowerShell Session

You can easily configure the jPowerShell session:

  • By project creating a jpowershell.properties file in the classpath of your project and settings the variables you want to override.
  • By call, using a map that can be chained to powershell call.

For example:

    //Set the timeout when waiting for command to terminate to 30 seconds instead of 10 (default value)
    Map<String, String> myConfig = new HashMap<>();
    myConfig.put("maxWait", "30000");
    response = powerShell.configuration(myConfig).executeCommand("Get-WmiObject Win32_BIOS");

The variables that can be configured in jPowerShell are:

waitPause: the pause in ms between each loop pooling for a response. Default value is 10

maxWait: the maximum wait in ms for the command to execute. Default value is 10000

tempFolder: if you set this variable jPowerShell will use this folder in order to store temporary the scripts to execute. By default the environment variable java.io.tmpdir will be used.

Advanced usage

Setting the PowerShell executable path

If the PowerShell executable has a different name/path on your system, you can change it when opening a new session:

    //Creates PowerShell session
    try (PowerShell powerShell = PowerShell.openSession("myCustomPowerShellExecutable.exe")) {
       [...]

Executing PowerShell Script

In order to execute a PowerShell Script it is recommended to use the executeScript() method instead of executeCommand():

   try (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
       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?
   }

Executing PowerShell Scripts packaged inside jar

In order to execute a PowerShell Script that is bundled inside a jar you must use a BufferedReader to load the resource:

    PowerShell powerShell = PowerShell.openSession();
    String script = "resourcePath/MyScript.ps1"
    String scriptParams = "-Parameter value"

    //Read the resource
    BufferedReader srcReader = new BufferedReader(
                    new InputStreamReader(getClass().getResourceAsStream(script)));

    if (scriptParams != null && !scriptParams.equals("")) {
        response = powerShell.executeScript(srcReader, scriptParams);
    } else {
        response =  powerShell.executeScript(srcReader);
    }

jpowershell's People

Contributors

agiles231 avatar harinus avatar mrge-org avatar pandareen avatar profesorfalken avatar sparsick avatar trackersb avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.