0

Having the following NET 5 Console application:

static void Main(string[] args)
{
    using var ps = PowerShell.Create();
    // ps.AddCommand("Get-Service");
    ps.AddStatement().AddCommand("Get-Service");
    ps.Invoke();
}

The calls seems to be executed with no error, but where the output goes? I've tried both AddCommand and AddStatement, and also examined the ps variable in debugger, no signs of output. I did discover the ps.Streams.Information but it is empty.

2
  • 2
    var results = ps.Invoke();. See the documentation Commented Feb 28, 2021 at 8:18
  • 1
    many thx, it's an answer Commented Feb 28, 2021 at 8:21

1 Answer 1

1

As detailed in the documentation for the PowerShell.Invoke method, it returns a collection containing any results that the PowerShell operation produced.

Thus, you can write

static void Main(string[] args)
{
    using var ps = PowerShell.Create();
    ps.AddStatement().AddCommand("Get-Service");
    var results = ps.Invoke();

    foreach (var result in results)
    {
        Console.WriteLine(result);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.