在c#中解析PowerShell输出

本文关键字:PowerShell 输出 | 更新日期: 2023-09-27 18:05:36

如何解析c#中powerShell控制台的输出?

PowerShell控制台输出:

Name        : 0F06B9DF-9FC5-4AF7-AF92-890AAD415000
ElementName : Virtual Machine 1
Name        : 2B501DD8-46F5-45FE-ACCE-62030720A000
ElementName : Virtual Machine 2

在我的c#代码中逐行读取输出。我搜索将输出转换为

的最佳方法。
List<Dictionary<string, string>> 

我试着

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>();
        }
    }
}

在c#中解析PowerShell输出

如果进程很快结束并且没有提供大量输出,那么等待它退出,然后立即读取所有标准输出流可能是一个好主意。

proc.WaitForExit((int)TimeSpan.FromSeconds(10).TotalMilliseconds);
var ret = new List<Dictionary<string, string>>();
// first split on double newline to separate VMs
string[] vms = proc.StandardOutput.ReadToEnd()
    .Split(new[] { string.Format("{0}{0}", Environment.NewLine) }, StringSplitOptions.RemoveEmptyEntries);
foreach (var vm in vms)
{
    // then split on newline, to get each line in separate string
    var lines = vm.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
    // finally, split each line on ':' to fill in dictionary, then add it to list
    ret.Add(lines.Select(line => line.Split(':')).ToDictionary(a => a[0].Trim(), a => a[1].Trim()));
}

您可能还希望将其包装在try...catch中,以防过程输出不是您期望的格式。

加油!c#和Powershell——都是。net语言。您可以从c#中调用Powershell脚本并以对象的形式获得输出。不需要解析任何内容:

    static void Main(string[] args)
    {
        var rsc = RunspaceConfiguration.Create();
        var runSpace = RunspaceFactory.CreateRunspace(rsc);
        runSpace.Open();
        Runspace.DefaultRunspace = runSpace;
        var p = runSpace.CreatePipeline("Get-Process");
        var output = p.Invoke();
        foreach (var o in output)
        {
            Console.WriteLine("{0}", o.Properties["Name"].Value);
        }
    }
  using (PowerShell PowerShellInstance = PowerShell.Create())
              {
                  // this script has a sleep in it to simulate a long running script
                  PowerShellInstance.AddScript("GET-VM");
                  //PowerShellInstance.AddCommand("Out-String");
                  // begin invoke execution on the pipeline
                  foreach (var o in PowerShellInstance.Invoke())
                  {
                      //Console.WriteLine(str);
                    Console.WriteLine("{0}", o.Properties["Name"].Value);
                    Console.WriteLine("{0}", o.Properties["Status"].Value);
                }
                  // do something else until execution has completed.
                  // this could be sleep/wait, or perhaps some other work
}