PowerShell
Class
Definition
Represents a PowerShell command or script to execute against a Runspace(Pool) if provided, otherwise execute using a default Runspace. Provides access to different result buffers like output, error, debug, verbose, progress and warning.
Provides a simple interface to execute a powershell command:
<pre><code>
Powershell.Create("get-process").Invoke();
</code></pre>
The above statetement creates a local runspace using default
configuration, executes the command and then closes the runspace.
Using RunspacePool property, the caller can provide the runspace
where the command / script is executed.
public sealed class PowerShell : IDisposable
- Inheritance
-
PowerShell
- Implements
Properties
| Commands |
Gets or sets current powershell command line. |
| HadErrors |
If an error occurred while executing the pipeline, this will be set to true. |
| HistoryString |
The history string to be used for displaying the history. |
| InstanceId |
Get unqiue id for this instance of runspace pool. It is primarily used for logging purposes. |
| InvocationStateInfo |
Gets the execution state of the current PowerShell instance. |
| IsNested |
Gets the property which indicates if this PowerShell instance is nested. |
| IsRunspaceOwner |
Indicates if this PowerShell object is the owner of the runspace or RunspacePool assigned to this object |
| Runspace |
Sets an associated Runspace for this PowerShell instance. This can be null in which case a new runspace is created whenever Invoke* method is called. |
| RunspacePool |
Sets an associated RunspacePool for this PowerShell instance. A Runspace from this pool is used whenever Invoke* method is called.
|
| Streams |
Streams generated by PowerShell invocations |
Methods
| AddArgument(Object) |
Adds an argument to the last added command. For example, to construct a command string "get-process | select-object name"
|
| AddCommand(CommandInfo) |
CommandInfo object for the command to add. |
| AddCommand(String) |
Add a cmdlet to construct a command pipeline. For example, to construct a command string "get-process | sort-object",
|
| AddCommand(String, Boolean) |
Add a cmdlet to construct a command pipeline. For example, to construct a command string "get-process | sort-object",
|
| AddParameter(String) |
Adds a switch parameter to the last added command. For example, to construct a command string "get-process | sort-object -descending"
|
| AddParameter(String, Object) |
Add a parameter to the last added command. For example, to construct a command string "get-process | select-object -property name"
|
| AddParameters(IDictionary) |
Adds a set of parameters to the last added command. |
| AddParameters(IList) |
Adds a set of parameters to the last added command. |
| AddScript(String) |
Add a piece of script to construct a command pipeline. For example, to construct a command string "get-process | foreach { $_.Name }"
|
| AddScript(String, Boolean) |
Add a piece of script to construct a command pipeline. For example, to construct a command string "get-process | foreach { $_.Name }"
|
| AddStatement() |
Adds an additional statement for execution
|
| AsJobProxy() |
Returns a job object which can be used to control the invocation of the command with AsJob Parameter |
| BeginInvoke() |
Invoke the Command asynchronously. Use EndInvoke() to obtain the output of the command. |
| BeginInvoke<T>(PSDataCollection<T>) |
Invoke the Command asynchronously. Use EndInvoke() to obtain the output of the command. |
| BeginInvoke<T>(PSDataCollection<T>, PSInvocationSettings, AsyncCallback, Object) |
Invoke the Command asynchronously. Use EndInvoke() to obtain the output of the command. |
| BeginInvoke<TInput,TOutput>(PSDataCollection<TInput>, PSDataCollection<TOutput>) |
Invoke the Command asynchronously. When this method is used EndInvoke() returns a null buffer. |
| BeginInvoke<TInput,TOutput>(PSDataCollection<TInput>, PSDataCollection<TOutput>, PSInvocationSettings, AsyncCallback, Object) |
Invoke the Command asynchronously and collect
output data into the buffer |
| BeginStop(AsyncCallback, Object) |
Stop the currently running command asynchronously. If the command is not started, the state of PowerShell instance is changed to Stopped and corresponding events will be raised.
|
| Connect() |
Synchronously connects to a running command on a remote server. |
| ConnectAsync() |
Asynchronously connects to a running command on a remote server. The returned IAsyncResult object can be used with EndInvoke() method to wait on command and/or get command returned data. |
| ConnectAsync(PSDataCollection<PSObject>, AsyncCallback, Object) | |
| Create(RunspaceMode) |
Constructs an empty PowerShell instance; a script or command must be added before invoking this instance |
| Create(InitialSessionState) |
Constructs an empty PowerShell instance; a script or command must be added before invoking this instance |
| Create() |
Constructs an empty PowerShell instance; a script or command must be added before invoking this instance |
| CreateNestedPowerShell() |
Creates a nested powershell within the current instance. Nested PowerShell is used to do simple operations like checking state of a variable while another command is using the runspace.
|
| Dispose() |
Dispose all managed resources. This will suppress finalizer on the object from getting called by calling System.GC.SuppressFinalize(this). |
| EndInvoke(IAsyncResult) |
Waits for the pending asynchronous BeginInvoke to complete. |
| EndStop(IAsyncResult) |
Waits for the pending asynchronous BeginStop to complete. |
| Invoke(IEnumerable, PSInvocationSettings) |
Invoke the Command synchronously and return the output PSObject collection. |
| Invoke(IEnumerable) |
Invoke the Command synchronously and return the output PSObject collection. |
| Invoke() |
Invoke the Command synchronously and return the output PSObject collection. |
| Invoke<T>() |
Invoke the Command synchronously and return the output. |
| Invoke<T>(IEnumerable) |
Invoke the Command synchronously and return the output. |
| Invoke<T>(IEnumerable, IList<T>) |
Invoke the Command synchronously and collect
output data into the buffer |
| Invoke<T>(IEnumerable, PSInvocationSettings) |
Invoke the Command synchronously and return the output. |
| Invoke<T>(IEnumerable, IList<T>, PSInvocationSettings) |
Invoke the Command synchronously and collect
output data into the buffer |
| Invoke<TInput,TOutput>(PSDataCollection<TInput>, PSDataCollection<TOutput>, PSInvocationSettings) |
Invoke the Command synchronously and stream
output data into the buffer |
| Stop() |
Stop the currently running command synchronously. |
Events
| InvocationStateChanged |
Event rasied when PowerShell Execution State Changes. |