Saving Powershell command output to listbox

一个人想着一个人 提交于 2019-12-11 06:28:05

问题


Trying to write some GUI program using c#.

I need to execute some Powershell commands (Like Get-ContentFilterPhrase) and add their output to list box. Everything I was able to get right now is just a (Collection) in the first line of my listbox.

Could anyone describe how to do that right, please.

Here's one of my attempts:

var rulelist = new List<string>();
        var rsConfig = RunspaceConfiguration.Create();
        using (var myRunSpace = RunspaceFactory.CreateRunspace(rsConfig))
        {
            PSSnapInException snapInException = null;
            var info = rsConfig.AddPSSnapIn("Microsoft.Exchange.Management.PowerShell.E2010", out snapInException);

            myRunSpace.Open();

            using (var ps = PowerShell.Create())
            {
                ps.AddScript("Get-ContentFilterPhrase | Where-Object {$_.Influence -Match 'BadWord'}| Format-Table -Property Phrase");
                var results = ps.Invoke();
                foreach (var result in results)
                {
                    rulelist.Add(result.ToString());
                }
            }
            listBox1.Items.Add(rulelist);
        }

来源:https://stackoverflow.com/questions/45791456/saving-powershell-command-output-to-listbox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!