Compartilhar via


PowerShell.InvokeAsync Method

Definition

Overloads

InvokeAsync()

Invoke a PowerShell command asynchronously. Use await to wait for the command to complete and obtain the output of the command.

InvokeAsync<T>(PSDataCollection<T>)

Invoke a PowerShell command asynchronously. Use await to wait for the command to complete and obtain the output of the command.

InvokeAsync<T>(PSDataCollection<T>, PSInvocationSettings, AsyncCallback, Object)

Invoke a PowerShell command asynchronously. Use await to wait for the command to complete and obtain the output of the command.

InvokeAsync<TInput,TOutput>(PSDataCollection<TInput>, PSDataCollection<TOutput>)

Invoke a PowerShell command asynchronously. Use await to wait for the command to complete and obtain the output of the command.

InvokeAsync<TInput,TOutput>(PSDataCollection<TInput>, PSDataCollection<TOutput>, PSInvocationSettings, AsyncCallback, Object)

Invoke a PowerShell command asynchronously and collect output data into the buffer output. Use await to wait for the command to complete and obtain the output of the command.

InvokeAsync()

Invoke a PowerShell command asynchronously. Use await to wait for the command to complete and obtain the output of the command.

public System.Threading.Tasks.Task<System.Management.Automation.PSDataCollection<System.Management.Automation.PSObject>> InvokeAsync ();
member this.InvokeAsync : unit -> System.Threading.Tasks.Task<System.Management.Automation.PSDataCollection<System.Management.Automation.PSObject>>
Public Function InvokeAsync () As Task(Of PSDataCollection(Of PSObject))

Returns

The output buffer created to hold the results of the asynchronous invoke.

Exceptions

Cannot perform the operation because the command is already started. Stop the command and try the operation again. (or) No command is added.

Object is disposed.

The running PowerShell pipeline was stopped. This occurs when Stop() or StopAsync(AsyncCallback, Object) is called.

Applies to

InvokeAsync<T>(PSDataCollection<T>)

Invoke a PowerShell command asynchronously. Use await to wait for the command to complete and obtain the output of the command.

public System.Threading.Tasks.Task<System.Management.Automation.PSDataCollection<System.Management.Automation.PSObject>> InvokeAsync<T> (System.Management.Automation.PSDataCollection<T> input);
member this.InvokeAsync : System.Management.Automation.PSDataCollection<'T> -> System.Threading.Tasks.Task<System.Management.Automation.PSDataCollection<System.Management.Automation.PSObject>>
Public Function InvokeAsync(Of T) (input As PSDataCollection(Of T)) As Task(Of PSDataCollection(Of PSObject))

Type Parameters

T

Type of the input buffer.

Parameters

input
PSDataCollection<T>

Input to the command. See remarks for more details.

Returns

The output buffer created to hold the results of the asynchronous invoke.

Exceptions

Cannot perform the operation because the command is already started. Stop the command and try the operation again. (or) No command is added.

Object is disposed.

The running PowerShell pipeline was stopped. This occurs when Stop() or StopAsync(AsyncCallback, Object) is called.

Remarks

When invoked using InvokeAsync, invocation doesn't finish until Input is closed. Caller of InvokeAsync must close the input buffer after all input has been written to input buffer. Input buffer is closed by calling Close() method.

If you want this command to execute as a standalone cmdlet (that is, using command-line parameters only), be sure to call Close() before calling InvokeAsync(). Otherwise, the command will be executed as though it had external input. If you observe that the command isn't doing anything, this may be the reason.

Applies to

InvokeAsync<T>(PSDataCollection<T>, PSInvocationSettings, AsyncCallback, Object)

Invoke a PowerShell command asynchronously. Use await to wait for the command to complete and obtain the output of the command.

public System.Threading.Tasks.Task<System.Management.Automation.PSDataCollection<System.Management.Automation.PSObject>> InvokeAsync<T> (System.Management.Automation.PSDataCollection<T> input, System.Management.Automation.PSInvocationSettings settings, AsyncCallback callback, object state);
member this.InvokeAsync : System.Management.Automation.PSDataCollection<'T> * System.Management.Automation.PSInvocationSettings * AsyncCallback * obj -> System.Threading.Tasks.Task<System.Management.Automation.PSDataCollection<System.Management.Automation.PSObject>>
Public Function InvokeAsync(Of T) (input As PSDataCollection(Of T), settings As PSInvocationSettings, callback As AsyncCallback, state As Object) As Task(Of PSDataCollection(Of PSObject))

Type Parameters

T

Type of the input buffer.

Parameters

input
PSDataCollection<T>

Input to the command. See remarks for more details.

settings
PSInvocationSettings

Invocation Settings.

callback
AsyncCallback

An AsyncCallback to call once the command is invoked. Note: when using this API in script, don't pass in a delegate that is cast from a script block. The callback could be invoked from a thread without a default Runspace and a delegate cast from a script block would fail in that case.

state
Object

A user supplied state to call the callback with.

Returns

The output buffer created to hold the results of the asynchronous invoke.

Exceptions

Cannot perform the operation because the command is already started. Stop the command and try the operation again. (or) No command is added.

Object is disposed.

The running PowerShell pipeline was stopped. This occurs when Stop() or StopAsync(AsyncCallback, Object) is called.

Remarks

When invoked using InvokeAsync, invocation doesn't finish until Input is closed. Caller of InvokeAsync must close the input buffer after all input has been written to input buffer. Input buffer is closed by calling Close() method.

If you want this command to execute as a standalone cmdlet (that is, using command-line parameters only), be sure to call Close() before calling InvokeAsync(). Otherwise, the command will be executed as though it had external input. If you observe that the command isn't doing anything, this may be the reason.

Applies to

InvokeAsync<TInput,TOutput>(PSDataCollection<TInput>, PSDataCollection<TOutput>)

Invoke a PowerShell command asynchronously. Use await to wait for the command to complete and obtain the output of the command.

public System.Threading.Tasks.Task<System.Management.Automation.PSDataCollection<System.Management.Automation.PSObject>> InvokeAsync<TInput,TOutput> (System.Management.Automation.PSDataCollection<TInput> input, System.Management.Automation.PSDataCollection<TOutput> output);
member this.InvokeAsync : System.Management.Automation.PSDataCollection<'Input> * System.Management.Automation.PSDataCollection<'Output> -> System.Threading.Tasks.Task<System.Management.Automation.PSDataCollection<System.Management.Automation.PSObject>>
Public Function InvokeAsync(Of TInput, TOutput) (input As PSDataCollection(Of TInput), output As PSDataCollection(Of TOutput)) As Task(Of PSDataCollection(Of PSObject))

Type Parameters

TInput

Type of input object(s) for the command invocation.

TOutput

Type of output object(s) expected from the command invocation.

Parameters

input
PSDataCollection<TInput>

Input to the command. See remarks for more details.

output
PSDataCollection<TOutput>

A buffer supplied by the user where output is collected.

Returns

The output buffer created to hold the results of the asynchronous invoke, or null if the caller provided their own buffer.

Exceptions

Cannot perform the operation because the command is already started. Stop the command and try the operation again. (or) No command is added.

Object is disposed.

The running PowerShell pipeline was stopped. This occurs when Stop() or StopAsync(AsyncCallback, Object) is called. To collect partial output in this scenario, supply a PSDataCollection<T> for the output parameter, and either add a handler for the DataAdding event or catch the exception and enumerate the object supplied for output.

Remarks

When invoked using InvokeAsync, invocation doesn't finish until Input is closed. Caller of InvokeAsync must close the input buffer after all input has been written to input buffer. Input buffer is closed by calling Close() method.

If you want this command to execute as a standalone cmdlet (that is, using command-line parameters only), be sure to call Close() before calling InvokeAsync(). Otherwise, the command will be executed as though it had external input. If you observe that the command isn't doing anything, this may be the reason.

Applies to

InvokeAsync<TInput,TOutput>(PSDataCollection<TInput>, PSDataCollection<TOutput>, PSInvocationSettings, AsyncCallback, Object)

Invoke a PowerShell command asynchronously and collect output data into the buffer output. Use await to wait for the command to complete and obtain the output of the command.

public System.Threading.Tasks.Task<System.Management.Automation.PSDataCollection<System.Management.Automation.PSObject>> InvokeAsync<TInput,TOutput> (System.Management.Automation.PSDataCollection<TInput> input, System.Management.Automation.PSDataCollection<TOutput> output, System.Management.Automation.PSInvocationSettings settings, AsyncCallback callback, object state);
member this.InvokeAsync : System.Management.Automation.PSDataCollection<'Input> * System.Management.Automation.PSDataCollection<'Output> * System.Management.Automation.PSInvocationSettings * AsyncCallback * obj -> System.Threading.Tasks.Task<System.Management.Automation.PSDataCollection<System.Management.Automation.PSObject>>
Public Function InvokeAsync(Of TInput, TOutput) (input As PSDataCollection(Of TInput), output As PSDataCollection(Of TOutput), settings As PSInvocationSettings, callback As AsyncCallback, state As Object) As Task(Of PSDataCollection(Of PSObject))

Type Parameters

TInput

Type of input object(s) for the command invocation.

TOutput

Type of output object(s) expected from the command invocation.

Parameters

input
PSDataCollection<TInput>

Input to the command. See remarks for more details.

output
PSDataCollection<TOutput>

A buffer supplied by the user where output is collected.

settings
PSInvocationSettings

Invocation Settings.

callback
AsyncCallback

An AsyncCallback to call once the command is invoked. Note: when using this API in script, don't pass in a delegate that is cast from a script block. The callback could be invoked from a thread without a default Runspace and a delegate cast from a script block would fail in that case.

state
Object

A user supplied state to call the callback with.

Returns

The output buffer created to hold the results of the asynchronous invoke, or null if the caller provided their own buffer.

Exceptions

Cannot perform the operation because the command is already started. Stop the command and try the operation again. (or) No command is added.

Object is disposed.

The running PowerShell pipeline was stopped. This occurs when Stop() or StopAsync(AsyncCallback, Object) is called. To collect partial output in this scenario, supply a PSDataCollection<T> for the output parameter, and either add a handler for the DataAdding event or catch the exception and use object supplied for output.

Remarks

When invoked using InvokeAsync, invocation doesn't finish until Input is closed. Caller of InvokeAsync must close the input buffer after all input has been written to input buffer. Input buffer is closed by calling Close() method.

If you want this command to execute as a standalone cmdlet (that is, using command-line parameters only), be sure to call Close() before calling InvokeAsync(). Otherwise, the command will be executed as though it had external input. If you observe that the command isn't doing anything, this may be the reason.

Applies to