java.lang.Object
com.tlcsdm.core.powershell.PowerShell
- 所有已实现的接口:
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.
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
-
字段概要
字段修饰符和类型字段说明private booleanprivate PrintWriterprivate static final Stringprivate static final Stringstatic final Stringstatic final Stringprivate static final Loggerprivate longprivate Processprivate longprivate booleanprivate Fileprivate ExecutorServiceprivate int -
构造器概要
构造器 -
方法概要
修饰符和类型方法说明private voidvoidclose()Closes all the resources used to maintain the PowerShell contextprivate booleancloseAndWait(Future<String> task) 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 classpathprivate FilecreateWriteTempFile(BufferedReader srcReader) executeCommand(String command) Execute a PowerShell command.executeCommandAndChain(String command, PowerShellResponseHandler... response) Allows to chain command executions providing a more fluent API.executeScript(BufferedReader srcReader) Execute the provided PowerShell script in PowerShell console and gets result.executeScript(BufferedReader srcReader, String params) Execute the provided PowerShell script in PowerShell console and gets result.executeScript(String scriptPath) Executed the provided PowerShell script in PowerShell console and gets result.executeScript(String scriptPath, String params) Executed the provided PowerShell script in PowerShell console and gets result.static PowerShellResponseexecuteSingleCommand(String command) Execute a single command in PowerShell consolscriptModee and gets resultprivate longgetPID()private FilegetTempFolder(String tempPath) private voidhandleResponse(PowerShellResponseHandler response, PowerShellResponse powerShellResponse) private PowerShellbooleanIndicates if the last executed command finished in errorstatic PowerShellCreates 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 PowerShellopenSession(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
-
字段详细资料
-
logger
-
p
-
pid
private long pid -
commandWriter
-
closed
private boolean closed -
threadpool
-
DEFAULT_WIN_EXECUTABLE
- 另请参阅:
-
DEFAULT_LINUX_EXECUTABLE
- 另请参阅:
-
waitPause
private int waitPause -
maxWait
private long maxWait -
tempFolder
-
scriptMode
private boolean scriptMode -
END_COMMAND_STRING
- 另请参阅:
-
END_COMMAND
- 另请参阅:
-
-
构造器详细资料
-
PowerShell
private PowerShell()
-
-
方法详细资料
-
configuration
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 classpathThe 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
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, callopenSession()method instead- 返回:
- an instance of the class
- 抛出:
PowerShellNotAvailableException- if PowerShell is not installed in the system
-
initalize
private PowerShell initalize(String powerShellExecutablePath) throws PowerShellNotAvailableException -
executeCommand
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
Execute a single command in PowerShell consolscriptModee and gets result- 参数:
command- the command to execute- 返回:
- response with the output of the command
-
executeCommandAndChain
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 executeresponse- optionally, the response can be handled in a closure- 返回:
- The
PowerShellinstance
-
handleResponse
private void handleResponse(PowerShellResponseHandler response, PowerShellResponse powerShellResponse) -
isLastCommandInError
public boolean isLastCommandInError()Indicates if the last executed command finished in error- 返回:
- boolean
-
executeScript
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
Executed the provided PowerShell script in PowerShell console and gets result.- 参数:
scriptPath- the full path of the scriptparams- the parameters of the script- 返回:
- response with the output of the command
-
executeScript
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
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
-
close
public void close()Closes all the resources used to maintain the PowerShell context- 指定者:
close在接口中AutoCloseable
-
closeAndWait
-
checkState
private void checkState() -
getPID
private long getPID() -
getTempFolder
-