I\'ve just started out with PS and are writing some C# classes which I need to test from within PS.
Please note that these classes are NOT CmdLets.
I found a solution:
var objs= new PSDataCollection {obj};
using (var ps = PowerShell.Create())
{
ps.Runspace.SessionStateProxy.SetVariable("objList", objs);
ps.AddScript(@"$objList| ForEach { $_.Run()}");
ps.AddCommand("Out-String");
var output = ps.Invoke();
var stringBuilder = new StringBuilder();
foreach (var obj in output)
{
stringBuilder.AppendLine(obj.ToString());
}
var result = stringBuilder.ToString().Trim();
//Evaluate result.
}