I am encountering a very strange behavior when using PowerShell in C#. When I execute
InitialSessionState s = InitialSessionState.CreateDefault2();
var ps = PowerShell.Create(s);
ps.AddCommand("Write-Information")
.AddArgument("<test>")
.Invoke();
// Writes 0. But why?
Console.WriteLine(PS.Streams.Information.Count);
no streams (Stream.Information, Stream.Error, ...) are captured. But when I use
ps.AddScript("Write-Information '<test>'")
.Invoke();
// Writes 1 as expected
Console.WriteLine(PS.Streams.Information.Count);
Everything works as expected. Can anybody explain this difference? Am I missing some conceptions? How can there be a difference? How can I capture the streams of a AddCommand invocation?
Thanks for any input on this!