在 C# 中模仿这个非常简单的 powershell 命令

Mimic this very simple powershell command in C#

试图模仿 C# 中的命令 Get-CimInstance CIM_ManagedSystemElement

string NamespacePath = "\\.\Root\CIMv2";
string ClassName = "CIM_ManagedSystemElement";

//Create ManagementClass
ManagementClass oClass = new ManagementClass(NamespacePath + ":" + ClassName);

//Get all instances of the class and enumerate them
foreach (ManagementObject oObject in oClass.GetInstances())
{
    //access a property of the Management object
    Console.WriteLine("Caption : {0}", oObject["Caption"]);
}

遗憾的是,没有按预期工作,希望得到一些帮助

谢谢

我也无法让您的代码正常工作,但与此同时,如果您需要解决方法,可以使用我根据一些在线文档编写的这个简单程序,从 C# 中使用 PowerShell API。它会给你一个你正在寻找的输出。您应该可以访问 OutputCollection_DataAdded 中的所有属性,因此如果您需要的不仅仅是标题,您可以在此处获取它。此外,在执行结束时,有一个 foreach() 循环将包含整个输出 collection 如果您需要对其进行处理。执行速度非常慢,所以我不得不让它异步工作。

    static void Main(string[] args)
    {
        using (PowerShell ps = PowerShell.Create())
        {
            ps.AddCommand("Get-CimInstance");
            ps.AddParameter("-ClassName", "CIM_ManagedSystemElement");

            var outputCollection = new PSDataCollection<PSObject>();
            outputCollection.DataAdded += OutputCollection_DataAdded;

            // invoke execution on the pipeline (collecting output)
            var async = ps.BeginInvoke<PSObject, PSObject>(null, outputCollection);

            // do something else until execution has completed.
            // this could be sleep/wait, or perhaps some other work
            while (async.IsCompleted == false)
            {
                Console.WriteLine("Waiting for pipeline to finish...");
                Thread.Sleep(1000);

                // might want to place a timeout here...
            }

            Console.WriteLine("Execution has stopped. The pipeline state: " + ps.InvocationStateInfo.State);

            // loop through each output object item
            foreach (PSObject outputItem in ps.EndInvoke(async))
            {
                // if null object was dumped to the pipeline during the script then a null
                // object may be present here. check for null to prevent potential NRE.
                if (outputItem != null)
                {
                    //TODO: do something with the output item 
                    // outputItem.BaseOBject
                }
            }

            Console.Read();
        }
    }

    private static void OutputCollection_DataAdded(object sender, DataAddedEventArgs e)
    {
        if (sender is PSDataCollection<PSObject>)
        {
            var output = (PSDataCollection<PSObject>)sender;

            // Handle the output item here
            var caption = output.Last().Properties["Caption"];
            if (caption != null)
            {
                Console.WriteLine($"Caption: {caption.Value}");
            }
        }
    }

你这样做(你必须添加 System.Management 命名空间)

因为 CIM_ManagedSystemElement 位于默认的 WMI 命名空间(即 Root\CIMV2),您不必在 ManagementObjectSearcher 中指定它.

此外,请确保您拥有支持的最低客户端 - Windows Vista

    string query = @"SELECT * FROM CIM_ManagedSystemElement";


    var moSearch = new ManagementObjectSearcher(query);
    var moCollection = moSearch.Get();

    foreach (ManagementObject mo in moCollection)
    {
        Console.WriteLine("Caption = " + mo["Caption"]);
    }

此外,我建议您使用 ORM 删除像 ORMi or Kexla

这样的样板代码