Package com.profesorfalken.jpowershell
Class PowerShell
- java.lang.Object
-
- com.profesorfalken.jpowershell.PowerShell
-
- All Implemented Interfaces:
java.lang.AutoCloseable
public class PowerShell extends java.lang.Object implements java.lang.AutoCloseableThis 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.StringEND_SCRIPT_STRING
-
Method Summary
Modifier and Type Method Description voidclose()Closes all the resources used to maintain the PowerShell contextPowerShellconfiguration(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 classpathPowerShellResponseexecuteCommand(java.lang.String command)Execute a PowerShell command.PowerShellexecuteCommandAndChain(java.lang.String command, com.profesorfalken.jpowershell.PowerShellResponseHandler... response)Allows to chain command executions providing a more fluent API.PowerShellResponseexecuteScript(java.io.BufferedReader srcReader)Execute the provided PowerShell script in PowerShell console and gets result.PowerShellResponseexecuteScript(java.io.BufferedReader srcReader, java.lang.String params)Execute the provided PowerShell script in PowerShell console and gets result.PowerShellResponseexecuteScript(java.lang.String scriptPath)Executed the provided PowerShell script in PowerShell console and gets result.PowerShellResponseexecuteScript(java.lang.String scriptPath, java.lang.String params)Executed the provided PowerShell script in PowerShell console and gets result.static PowerShellResponseexecuteSingleCommand(java.lang.String command)Execute a single command in PowerShell consolscriptModee and gets resultbooleanisLastCommandInError()Indicates if the last executed command finished in errorstatic PowerShellopenSession()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 PowerShellopenSession(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
-
-
-
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 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
- 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, callopenSession()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 executeresponse- optionally, the response can be handled in a closure- Returns:
- The
PowerShellinstance
-
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 scriptparams- 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:
closein interfacejava.lang.AutoCloseable
-
-