C# 使用预先创建的对象调用 Powershell

发布于 2024-12-14 00:13:36 字数 778 浏览 3 评论 0原文

我刚刚开始使用 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”。

但我什至不确定这是否可能(可能是出于安全原因)。

我想我有两个选择:

  1. 这都是可能的。如果是这样,请帮助我:)?

  2. 我必须根据现有对象生成脚本文件,并执行“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:

  1. Either this is possible. If so please help me :) ?

  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

与他有关 2024-12-21 00:13:36

我找到了一个解决方案:

        var objs= new PSDataCollection<CustomObj> {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.
        }

I found a solution:

        var objs= new PSDataCollection<CustomObj> {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.
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文