C# 使用预先创建的对象调用 Powershell
我刚刚开始使用 PS,正在编写一些 C# 类,我需要在 PS 中测试这些类。
请注意,这些类不是 CmdLets。
我想做这样的事情:
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);
在传递给 PS 的对象上调用 Run() 时,结果将是打印输出“Hello world”。
但我什至不确定这是否可能(可能是出于安全原因)。
我想我有两个选择:
这都是可能的。如果是这样,请帮助我:)?
我必须根据现有对象生成脚本文件,并执行“AddScript(...)”以使 ps 执行它。< /strong>
任何指向我的指针开始就好了:)。
亲切的问候。
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);
Where I invoke Run() on the object handed to PS, the result would be a printout "Hello world".
But i'm not even sure that this is possible (might be for security reasons).
I figure I've got 2 options:
Either this is possible. If so please help me :) ?
I will have to generate the script file based on my existing object and the do an "AddScript(...)" to have ps execute it.
Any pointers to get me started would be nice :).
Kind regards.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我找到了一个解决方案:
I found a solution: