public List<string> getBlockedSenders()
{
RunspaceConfiguration rsConfig = RunspaceConfiguration.Create();
PSSnapInException snapInException = null;
PSSnapInInfo info = rsConfig.AddPSSnapIn("Microsoft.Exchange.Management.PowerShell.E2010", out snapInException);
Runspace myRunSpace = RunspaceFactory.CreateRunspace(rsConfig);
myRunSpace.Open();
Pipeline ps = myRunSpace.CreatePipeline();
Command getSenderFilterConfigCommand = new Command("Get-SenderFilterConfig");
ps.Commands.Add(getSenderFilterConfigCommand);
Command selectObjectCommand = new Command("Select-Object");
CommandParameter selectObjectArgs = new CommandParameter("ExpandProperty", "BlockedSenders");
selectObjectCommand.Parameters.Add(selectObjectArgs);
ps.Commands.Add(selectObjectCommand);
Command outStringCommand = new Command("Out-String");
CommandParameter outStringArgs = new CommandParameter("Stream");
outStringCommand.Parameters.Add(outStringArgs);
ps.Commands.Add(outStringCommand);
List<string> output = new List<string>();
foreach (PSObject result in ps.Invoke())
{
output.Add(result.ToString());
}
myRunSpace.Dispose();
return output;
}
Thursday, April 9, 2015
Example of how to call Powershell Pipeline in C# and bring in Exchange SnapIn
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment