简体   繁体   English

在集成Powershell和C#时遇到问题

[英]Having trouble integrating powershell and c#

EDIT: I figured out how to solve the specific code broken part of my question (see my answer bellow), but I'm still looking for resources on integrating powershell and C# so please still feel free to comment or answer! 编辑:我想出了如何解决我的问题的特定代码损坏部分(请参见下面的答案),但是我仍在寻找集成Powershell和C#的资源​​,因此请随时发表评论或回答!

I found a simple example of making your C# objects visible to powershell scripts and I've been playing around with it. 我找到了一个使C#对象对Powershell脚本可见的简单示例 ,并且我一直在研究它。

Using the following code: 使用以下代码:

  public partial class MainWindow : Window
        {
            public string MyName = "Evan";

            public MainWindow()
            {
                InitializeComponent();
                MessageBox.Show(RunScript("$DemoForm | Get-Member"));
                MessageBox.Show(RunScript("$DemoForm.MyName"));
                MessageBox.Show(RunScript("$DemoForm.Title"));
            }

            private string RunScript(string scriptText)
            {
                // create Powershell runspace

                Runspace runspace = RunspaceFactory.CreateRunspace();

                // open it

                runspace.Open();
                runspace.SessionStateProxy.SetVariable("DemoForm", this);
                // create a pipeline and feed it the script text

                Pipeline pipeline = runspace.CreatePipeline();
                pipeline.Commands.AddScript(scriptText);

                // add an extra command to transform the script
                // output objects into nicely formatted strings

                // remove this line to get the actual objects
                // that the script returns. For example, the script

                // "Get-Process" returns a collection
                // of System.Diagnostics.Process instances.

                pipeline.Commands.Add("Out-String");

                // execute the script

                Collection<PSObject> results = pipeline.Invoke();

                // close the runspace

                runspace.Close();

                // convert the script result into a single string

                StringBuilder stringBuilder = new StringBuilder();
                foreach (PSObject obj in results)
                {
                    stringBuilder.AppendLine(obj.ToString());
                }

                return stringBuilder.ToString();
            }


}

I get the expected results from these two lines: 我从这两行中得到了预期的结果:

MessageBox.Show(RunScript("$DemoForm | Get-Member"));
MessageBox.Show(RunScript("$DemoForm.Evan"));

But this line doesn't work (no error, it just returns an empty string): 但是此行不起作用(没有错误,它只返回一个空字符串):

MessageBox.Show(RunScript("$DemoForm.Title"));

Any idea why the first two work but not the third? 知道为什么前两个有效但第三个无效吗? Does it have something to do with threading (Having to access certain gui things from an sta thread?)? 它与线程有关(必须从sta线程访问某些gui东西吗?)? It seems like similar functionality was working with WindowsForms for the poster of the sample code. 似乎类似的功能正在与WindowsForms一起使用,作为示例代码的发布者。

Also, besides that example I linked to and this one here I haven't been able to find many resources online about linking c# and powershell. 另外,除了我链接到的那个示例之外, 在这里的这个我还无法在线找到许多有关链接c#和powershell的资源。 Ultimately I'm trying to create an application that will be scriptable via powershell - does anyone know of other good online resources or a good book that covers this? 最终,我试图创建一个可通过Powershell编写脚本的应用程序-是否有人知道其他优秀的在线资源或涵盖此方面的好书?

Thanks!!!! 谢谢!!!!

I got it! 我知道了! (with help from this video ). (在此视频的帮助下)。 The above code needs this line to work 上面的代码需要这一行才能工作

runspace.ThreadOptions = PSThreadOptions.UseCurrentThread

Which makes sense to me, I've always had trouble with STA threads and all that jaz :). 这对我来说很有意义,我一直在STA线程和所有jaz :)上遇到麻烦。

应该不是.Text而不是.Title吗?

MessageBox.Show(RunScript("$DemoForm.Text"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM