WQL在C#和PowerShell上返回不同的结果

时间:2017-05-21 06:20:12

标签: wmi wmi-query

我在C#和PowerShell上运行此WQL“SELECT * FROM Win32_Service WHERE Started = true”,但返回的结果不同。通过C#进行查询时,列表中找不到某些处于运行模式的服务。

C#代码:

 ManagementObjectSearcher s1 = new ManagementObjectSearcher("SELECT * FROM Win32_Service WHERE Started=true");
        ManagementObjectCollection qCollection = s1.Get();
        foreach (ManagementObject mo in qCollection)
        {
            Console.WriteLine("Service: '{0}'", mo["Name"]);
        }

PowerShell代码:

$query = "SELECT * FROM Win32_Service WHERE Started=true" $results = Get-WmiObject -Query $query $results|Export-Csv -path .\temp\service.csv -Encoding ASCII -NoTypeInformation

1 个答案:

答案 0 :(得分:0)

在C#代码中添加了模拟并立即解决了问题。 To connect to WMI remotely with C# (System.Management)