The way i would consider doing it is by using a piece of PowerShell script and then 'playing' with the output in C#.
If you add a reference to the following item you will be able to interact with PowerShell scripts in C# :
System.Management.Automation
Then use the following using statements to delve into and interact with the features of this :
using System.Collections.ObjectModel;
using System.Management.Automation;
using System.Management.Automation.Runspaces
The following script will create a nice sub that will take a PowerShell command and return a readable string, with each item (in this case, a role) added as a new line :
private string RunScript(string scriptText)
{
// create a Powershell runspace then open it
Runspace runspace = RunspaceFactory.CreateRunspace();
runspace.Open();
// create a pipeline and add it to the text of the script
Pipeline pipeline = runspace.CreatePipeline();
pipeline.Commands.AddScript(scriptText);
// format the output into a readable string, rather than using Get-Process
// and returning the system.diagnostic.process
pipeline.Commands.Add("Out-String");
// execute the script and close the runspace
Collection<psobject /> results = pipeline.Invoke();
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();
}
Then you can pass the following PowerShell command to the script and receieve the output like so :
RunScript("Import-module servermanager | get-windowsfeature");
Alternatively you could just run this PowerShell command from a C# script and then read the output text file from C# when the script has finished processing :
import-module servermanager | get-windowsfeature > C:\output.txt
Hope this helps!