How can parse the output of powerShell console in C#?
PowerShell console output:
Name : 0F06B9DF-9FC5-4AF7-AF92-890AAD415000
ElementName : Virtual Machine 1
Name : 2B501DD8-46F5-45FE-ACCE-62030720A000
ElementName : Virtual Machine 2
In my C# code read the output line by line. I search the best way to convert the output to the
List<Dictionary<string, string>>
I tried
bool dataset = false;
Dictionary<string, string> data = new Dictionary<string, string>();
proc.Start();
while (!proc.StandardOutput.EndOfStream)
{
// The line of Output
string line = proc.StandardOutput.ReadLine();
// If line is not empty save data in dictionary
if(line.Length != 0)
{
dataset = true;
string[] vals = line.Split(':');
data.Add(vals[0], vals[1]);
}
// Else, save dictionary in list and reinit dictionary
else
{
// Next object
if (dataset)
{
arr.Add(data);
dataset = false;
data = new Dictionary<string, string>();
}
}
}