Class PowerShell

  • All Implemented Interfaces:
    java.lang.AutoCloseable

    public class PowerShell
    extends java.lang.Object
    implements java.lang.AutoCloseable
    This API allows to open a session into PowerShell console and launch different commands.
    This class cannot be instantiated directly. Please use instead the method PowerShell.openSession() and call the commands using the returned instance.

    Once the session is finished it should be closed in order to free resources. For doing that, you can either call manually close() or implement a try with resources as it implements AutoCloseable.

    Author:
    Javier Garcia Alonso
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.lang.String END_SCRIPT_STRING  
    • Method Summary

      Modifier and Type Method Description
      void close()
      Closes all the resources used to maintain the PowerShell context
      PowerShell configuration​(java.util.Map<java.lang.String,​java.lang.String> config)
      Allows to override jPowerShell configuration using a map of key/value
      Default values are taken from file jpowershell.properties, which can be replaced just setting it on project classpath
      PowerShellResponse executeCommand​(java.lang.String command)
      Execute a PowerShell command.
      PowerShell executeCommandAndChain​(java.lang.String command, com.profesorfalken.jpowershell.PowerShellResponseHandler... response)
      Allows to chain command executions providing a more fluent API.
      PowerShellResponse executeScript​(java.io.BufferedReader srcReader)
      Execute the provided PowerShell script in PowerShell console and gets result.
      PowerShellResponse executeScript​(java.io.BufferedReader srcReader, java.lang.String params)
      Execute the provided PowerShell script in PowerShell console and gets result.
      PowerShellResponse executeScript​(java.lang.String scriptPath)
      Executed the provided PowerShell script in PowerShell console and gets result.
      PowerShellResponse executeScript​(java.lang.String scriptPath, java.lang.String params)
      Executed the provided PowerShell script in PowerShell console and gets result.
      static PowerShellResponse executeSingleCommand​(java.lang.String command)
      Execute a single command in PowerShell consolscriptModee and gets result
      boolean isLastCommandInError()
      Indicates if the last executed command finished in error
      static PowerShell openSession()
      Creates a session in PowerShell console an returns an instance which allows to execute commands in PowerShell context.
      It uses the default PowerShell installation in the system.
      static PowerShell openSession​(java.lang.String customPowerShellExecutablePath)
      Creates a session in PowerShell console an returns an instance which allows to execute commands in PowerShell context.
      This method allows to define a PowersShell executable path different from default
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • END_SCRIPT_STRING

        public static final java.lang.String END_SCRIPT_STRING
        See Also:
        Constant Field Values
    • Method Detail

      • configuration

        public PowerShell configuration​(java.util.Map<java.lang.String,​java.lang.String> config)
        Allows to override jPowerShell configuration using a map of key/value
        Default values are taken from file jpowershell.properties, which can be replaced just setting it on project classpath

        The values that can be overridden 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
        Parameters:
        config - map with the configuration in key/value format
        Returns:
        instance to chain
      • openSession

        public static PowerShell openSession()
                                      throws PowerShellNotAvailableException
        Creates a session in PowerShell console an returns an instance which allows to execute commands in PowerShell context.
        It uses the default PowerShell installation in the system.
        Returns:
        an instance of the class
        Throws:
        PowerShellNotAvailableException - if PowerShell is not installed in the system
      • openSession

        public static PowerShell openSession​(java.lang.String customPowerShellExecutablePath)
                                      throws PowerShellNotAvailableException
        Creates a session in PowerShell console an returns an instance which allows to execute commands in PowerShell context.
        This method allows to define a PowersShell executable path different from default
        Parameters:
        customPowerShellExecutablePath - the path of powershell executable. If you are using the default installation path, call openSession() method instead
        Returns:
        an instance of the class
        Throws:
        PowerShellNotAvailableException - if PowerShell is not installed in the system
      • executeCommand

        public PowerShellResponse executeCommand​(java.lang.String command)
        Execute a PowerShell command.

        This method launch a thread which will be executed in the already created PowerShell console context

        Parameters:
        command - the command to call. Ex: dir
        Returns:
        PowerShellResponse the information returned by powerShell
      • executeSingleCommand

        public static PowerShellResponse executeSingleCommand​(java.lang.String command)
        Execute a single command in PowerShell consolscriptModee and gets result
        Parameters:
        command - the command to execute
        Returns:
        response with the output of the command
      • executeCommandAndChain

        public PowerShell executeCommandAndChain​(java.lang.String command,
                                                 com.profesorfalken.jpowershell.PowerShellResponseHandler... response)
        Allows to chain command executions providing a more fluent API.

        This method allows also to optionally handle the response in a closure

        Parameters:
        command - the command to execute
        response - optionally, the response can be handled in a closure
        Returns:
        The PowerShell instance
      • isLastCommandInError

        public boolean isLastCommandInError()
        Indicates if the last executed command finished in error
        Returns:
        boolean
      • executeScript

        public PowerShellResponse executeScript​(java.lang.String scriptPath)
        Executed the provided PowerShell script in PowerShell console and gets result.
        Parameters:
        scriptPath - the full path of the script
        Returns:
        response with the output of the command
      • executeScript

        public PowerShellResponse executeScript​(java.lang.String scriptPath,
                                                java.lang.String params)
        Executed the provided PowerShell script in PowerShell console and gets result.
        Parameters:
        scriptPath - the full path of the script
        params - the parameters of the script
        Returns:
        response with the output of the command
      • executeScript

        public PowerShellResponse executeScript​(java.io.BufferedReader srcReader)
        Execute the provided PowerShell script in PowerShell console and gets result.
        Parameters:
        srcReader - the script as BufferedReader (when loading File from jar)
        Returns:
        response with the output of the command
      • executeScript

        public PowerShellResponse executeScript​(java.io.BufferedReader srcReader,
                                                java.lang.String params)
        Execute the provided PowerShell script in PowerShell console and gets result.
        Parameters:
        srcReader - the script as BufferedReader (when loading File from jar)
        params - the parameters of the script
        Returns:
        response with the output of the command
      • close

        public void close()
        Closes all the resources used to maintain the PowerShell context
        Specified by:
        close in interface java.lang.AutoCloseable