我试图查询root\CIMV2名称空间中的所有WMI类的名称.有没有办法使用powershell命令在C#中检索此信息?
我不确定你为什么提到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);
}
Run Code Online (Sandbox Code Playgroud)
沿着基思的方法
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);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)