问题
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