Powershell C# asynchronous execution

Sridhar

I want to execute a Powershell script asynchronously in C#. I've used BeginInvoke method but I can't get the output/errors even though I've attached delegates for them. Here is my C# code:

using(ps=PowerShell.Create())
{
    PSDataCollection<PSObject> output=new PSDataCollection<PSObject>();
    output.DataAdded+=output_DataAdded;
    ps.AddScript(RenewalScript);
    SecureString ss = new SecureString();
    for (int i = 0; i < Password.Length; i++)
        ss.AppendChar(Password.ElementAt(i));

    PSCredential cred = new PSCredential(Username, ss);
    ps.AddParameter("Credential", cred);
    ps.AddParameter("BuildServer", Server);
    ps.AddParameter("CAServer", CAServer);
    ps.AddParameter("CAName", CAName);
    ps.Streams.Error.DataAdded += Error_DataAdded;
    var result=ps.BeginInvoke<PSObject, PSObject>(null, output);

}

void output_DataAdded(object sender, DataAddedEventArgs e)
{
    //display output in UI
}

void Error_DataAdded(object sender, DataAddedEventArgs e)
{
    //display error message in UI
}

I have multiple Write-Output statements in my script. However, the DataAdded methods are never triggered. But they are triggered when I use the synchronous Invoke method.

Anatoly Ruchka

See this answer. To actually run it asynchronously, you need to make your method asynchronous too. You can do that by calling Task.Factory.FromAsync(...) to get a Task<PSObject> for the asynchronous operation, then using await.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

iOS Swift: order of code execution / asynchronous execution

分類Dev

PySide Application with asynchronous function execution

分類Dev

Execution of asynchronous code like Promises under the hood

分類Dev

Generic Asynchronous Java Job Execution Framework / Libraryの検索

分類Dev

Node JS Asynchronous Execution and Event Emission / Listener Models

分類Dev

C# Weird execution sequence

分類Dev

C# asynchronous threading API crawler

分類Dev

How can you execute initialization before powershell script execution?

分類Dev

C# vs PowerShell

分類Dev

C# An asynchronous operation cannot be started at this time.

分類Dev

Using Task<List<Task>> for asynchronous recursive operation in C#

分類Dev

Can I implement c# task asynchronous with java(type)script?

分類Dev

Asynchronous call in C# using Oracle.DataAccess (ODAC)

分類Dev

Asp.net C# Asynchronous call to Web Service method

分類Dev

C# & Powershell: Not returning all Powershell results

分類Dev

C++ Displaying the beginning time and ending time of a program execution

分類Dev

Optimized c++ function for nth prime number execution time

分類Dev

How to reduce execution time differences between C API and Python executable?

分類Dev

Unable to run compiled C++ programs on first execution

分類Dev

How to reduce execution time in C++ for the following code?

分類Dev

C++: Control order of thread execution with mutex's and conditional variables

分類Dev

Why execution stops after return statement in c++?

分類Dev

How to use powershell variable in PowerShell.AddParameter method in C#?

分類Dev

Is createreadstream asynchronous?

分類Dev

Chain asynchronous operations that return Either using Language-Ext in C#

分類Dev

Visual Studio - program ends before asynchronous tasks can finish - C#

分類Dev

Trying to recreate Microsoft's .net Asynchronous Client/Server Socket Example in UWP/C#

分類Dev

Invoking PowerShell cmdlets more quickly in C#

分類Dev

Memory leaks in C# hosted Powershell

Related 関連記事

  1. 1

    iOS Swift: order of code execution / asynchronous execution

  2. 2

    PySide Application with asynchronous function execution

  3. 3

    Execution of asynchronous code like Promises under the hood

  4. 4

    Generic Asynchronous Java Job Execution Framework / Libraryの検索

  5. 5

    Node JS Asynchronous Execution and Event Emission / Listener Models

  6. 6

    C# Weird execution sequence

  7. 7

    C# asynchronous threading API crawler

  8. 8

    How can you execute initialization before powershell script execution?

  9. 9

    C# vs PowerShell

  10. 10

    C# An asynchronous operation cannot be started at this time.

  11. 11

    Using Task<List<Task>> for asynchronous recursive operation in C#

  12. 12

    Can I implement c# task asynchronous with java(type)script?

  13. 13

    Asynchronous call in C# using Oracle.DataAccess (ODAC)

  14. 14

    Asp.net C# Asynchronous call to Web Service method

  15. 15

    C# & Powershell: Not returning all Powershell results

  16. 16

    C++ Displaying the beginning time and ending time of a program execution

  17. 17

    Optimized c++ function for nth prime number execution time

  18. 18

    How to reduce execution time differences between C API and Python executable?

  19. 19

    Unable to run compiled C++ programs on first execution

  20. 20

    How to reduce execution time in C++ for the following code?

  21. 21

    C++: Control order of thread execution with mutex's and conditional variables

  22. 22

    Why execution stops after return statement in c++?

  23. 23

    How to use powershell variable in PowerShell.AddParameter method in C#?

  24. 24

    Is createreadstream asynchronous?

  25. 25

    Chain asynchronous operations that return Either using Language-Ext in C#

  26. 26

    Visual Studio - program ends before asynchronous tasks can finish - C#

  27. 27

    Trying to recreate Microsoft's .net Asynchronous Client/Server Socket Example in UWP/C#

  28. 28

    Invoking PowerShell cmdlets more quickly in C#

  29. 29

    Memory leaks in C# hosted Powershell

ホットタグ

アーカイブ