C# Invoke Powershell with pre-created object
问题 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 want to do something like this: var myCustomObj = new CustomObj { Message = "Hello world" }; var ps = Powershell.Create(); ps.AddCommand("Import-Module").AddParameter("Assembly", "MyCustomAsm"); ps.AddCommand("myCustomObj.Run()").AddParameter(myCustomObj); foreach(string str in ps.AddCommand("Out-String").Invoke<string>()) Console.WriteLine(str);