类 PowerShell

java.lang.Object
com.tlcsdm.core.powershell.PowerShell
所有已实现的接口:
AutoCloseable

public class PowerShell extends Object implements 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.

作者:
Javier Garcia Alonso
  • 字段详细资料

    • logger

      private static final Logger logger
    • p

      private Process p
    • pid

      private long pid
    • commandWriter

      private PrintWriter commandWriter
    • closed

      private boolean closed
    • threadpool

      private ExecutorService threadpool
    • DEFAULT_WIN_EXECUTABLE

      private static final String DEFAULT_WIN_EXECUTABLE
      另请参阅:
    • DEFAULT_LINUX_EXECUTABLE

      private static final String DEFAULT_LINUX_EXECUTABLE
      另请参阅:
    • waitPause

      private int waitPause
    • maxWait

      private long maxWait
    • tempFolder

      private File tempFolder
    • scriptMode

      private boolean scriptMode
    • END_COMMAND_STRING

      public static final String END_COMMAND_STRING
      另请参阅:
    • END_COMMAND

      public static final String END_COMMAND
      另请参阅:
  • 构造器详细资料

    • PowerShell

      private PowerShell()
  • 方法详细资料

    • configuration

      public PowerShell configuration(Map<String,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
      参数:
      config - map with the configuration in key/value format
      返回:
      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.
      返回:
      an instance of the class
      抛出:
      PowerShellNotAvailableException - if PowerShell is not installed in the system
    • openSession

      public static PowerShell openSession(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
      参数:
      customPowerShellExecutablePath - the path of powershell executable. If you are using the default installation path, call openSession() method instead
      返回:
      an instance of the class
      抛出:
      PowerShellNotAvailableException - if PowerShell is not installed in the system
    • initalize

      private PowerShell initalize(String powerShellExecutablePath) throws PowerShellNotAvailableException
      抛出:
      PowerShellNotAvailableException
    • executeCommand

      public PowerShellResponse executeCommand(String command)
      Execute a PowerShell command.

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

      参数:
      command - the command to call. Ex: dir
      返回:
      PowerShellResponse the information returned by powerShell
    • executeSingleCommand

      public static PowerShellResponse executeSingleCommand(String command)
      Execute a single command in PowerShell consolscriptModee and gets result
      参数:
      command - the command to execute
      返回:
      response with the output of the command
    • executeCommandAndChain

      public PowerShell executeCommandAndChain(String command, PowerShellResponseHandler... response)
      Allows to chain command executions providing a more fluent API.

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

      参数:
      command - the command to execute
      response - optionally, the response can be handled in a closure
      返回:
      The PowerShell instance
    • handleResponse

      private void handleResponse(PowerShellResponseHandler response, PowerShellResponse powerShellResponse)
    • isLastCommandInError

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

      public PowerShellResponse executeScript(String scriptPath)
      Executed the provided PowerShell script in PowerShell console and gets result.
      参数:
      scriptPath - the full path of the script
      返回:
      response with the output of the command
    • executeScript

      public PowerShellResponse executeScript(String scriptPath, String params)
      Executed the provided PowerShell script in PowerShell console and gets result.
      参数:
      scriptPath - the full path of the script
      params - the parameters of the script
      返回:
      response with the output of the command
    • executeScript

      public PowerShellResponse executeScript(BufferedReader srcReader)
      Execute the provided PowerShell script in PowerShell console and gets result.
      参数:
      srcReader - the script as BufferedReader (when loading File from jar)
      返回:
      response with the output of the command
    • executeScript

      public PowerShellResponse executeScript(BufferedReader srcReader, String params)
      Execute the provided PowerShell script in PowerShell console and gets result.
      参数:
      srcReader - the script as BufferedReader (when loading File from jar)
      params - the parameters of the script
      返回:
      response with the output of the command
    • createWriteTempFile

      private File createWriteTempFile(BufferedReader srcReader)
    • close

      public void close()
      Closes all the resources used to maintain the PowerShell context
      指定者:
      close 在接口中 AutoCloseable
    • closeAndWait

      private boolean closeAndWait(Future<String> task) throws InterruptedException, ExecutionException
      抛出:
      InterruptedException
      ExecutionException
    • checkState

      private void checkState()
    • getPID

      private long getPID()
    • getTempFolder

      private File getTempFolder(String tempPath)