Get Powershell errors from c#

Question:

Problem

I am invoking powershell commands from c# however, the PowerShell command object only seems to have the property bool HasErrors which doesn’t help me know what error I received.

This is how I build my powershell command

Library

Invoking Move-Vm cmdlet

I was expecting some sort of exception to be thrown on failure, but instead it returns 0 objects. While HasErrors will result in knowing if the command was successful or not; I’m still not sure how to get the specific error, as no exception is thrown.

Thanks

Answer:

To see the errors look at the collection PowerShell.Streams.Error or in your code command.Streams.Error.

Source:

Get Powershell errors from c# by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply