【问题标题】:Powershell Command in C#C# 中的 Powershell 命令
【发布时间】:2010-12-28 08:02:30
【问题描述】:

我正在尝试查询 root\CIMV2 命名空间中所有 WMI 类的名称。有没有办法使用 powershell 命令在 C# 中检索这些信息?

【问题讨论】:

    标签: c# powershell wmi


    【解决方案1】:

    按照基思的方法

    using System;
    using System.Management.Automation;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                var script = @" 
                    Get-WmiObject -list -namespace root\cimv2 | Foreach {$_.Name}
                ";
    
                var powerShell = PowerShell.Create();
                powerShell.AddScript(script);
    
                foreach (var className in powerShell.Invoke())
                {
                    Console.WriteLine(className);
                }
            }
        }
    }
    

    【讨论】:

      【解决方案2】:

      我不确定你为什么提到 PowerShell;您可以在纯 C# 和 WMI(即 System.Management 命名空间)中执行此操作。

      要获取所有 WMI 类的列表,请使用 SELECT * FROM Meta_Class 查询:

      using System.Management;
      ...
      
      try
      {
          EnumerationOptions options = new EnumerationOptions();
          options.ReturnImmediately = true;
          options.Rewindable = false;
      
          ManagementObjectSearcher searcher =
              new ManagementObjectSearcher("root\\cimv2", "SELECT * FROM Meta_Class", options);
      
          ManagementObjectCollection classes = searcher.Get();
      
          foreach (ManagementClass cls in classes)
          {
              Console.WriteLine(cls.ClassPath.ClassName);
          }
      }
      catch (ManagementException exception)
      {
          Console.WriteLine(exception.Message);
      }
      

      【讨论】:

        【解决方案3】:

        我个人会采用 Helen 的方法并消除对 PowerShell 的依赖。也就是说,以下是您在 C# 中编写代码以使用 PowerShell 检索所需信息的方法:

        using System;
        using System.Collections.Generic;
        using System.Collections.ObjectModel;
        using System.Linq;
        using System.Management.Automation;
        
        namespace RunspaceInvokeExp
        {
            class Program
            {
                static void Main()
                {
                    using (var invoker = new RunspaceInvoke())
                    {
                        string command = @"Get-WmiObject -list -namespace root\cimv2" +
                                          " | Foreach {$_.Name}";
                        Collection<PSObject> results = invoker.Invoke(command);
                        var classNames = results.Select(ps => (string)ps.BaseObject);
                        foreach (var name in classNames)
                        {
                            Console.WriteLine(name);
                        }
                    }
                }
            }
        }
        

        【讨论】:

          【解决方案4】:

          请注意,有一个工具可让您创建、运行和保存用 PowerShell 编写的 WMI 脚本,即PowerShell Scriptomatic 工具,可从Microsoft TechNet 站点下载。

          使用此工具,您可以探索 root\CIMV2 或任何其他 WMI 命名空间中的所有 WMI 类。

          【讨论】:

            【解决方案5】:

            您可能只想使用 Helen 回答的 System.Management 命名空间,但您也可以在应用程序中托管 powershell。见http://www.codeproject.com/KB/cs/HowToRunPowerShell.aspx

            【讨论】:

              猜你喜欢
              • 2013-09-29
              • 2011-03-02
              • 2011-06-04
              • 2010-11-08
              • 2016-05-18
              • 2023-04-06
              • 2013-11-21
              • 1970-01-01
              • 2012-05-21
              相关资源
              最近更新 更多