WMI不返回预期的服务,ServiceController标识为“确定”

时间:2018-12-07 13:34:47

标签: c# wmi servicecontroller

我正在尝试确定远程PC上的某项服务是否正在运行,并确定其启动类型。

使用ServiceController,我可以在远程计算机上成功看到预期的服务,但是当切换为使用WMI进行更深入的研究时,该服务不再显示。

这里是我的代码:

    public static void Main()
    {
        var ctl = ServiceController.GetServices("[Name]");
        List<string> namelist = new List<string>();

        foreach (var x in ctl)
        {
            if (x.DisplayName == "NHS Card Checker")
            {
                Console.WriteLine(string.Format("NHS Card checker found on MPC - Status: {0}", x.Status));

                ConnectionOptions options = new ConnectionOptions();
                options.Impersonation = System.Management.ImpersonationLevel.Impersonate;
                ManagementScope scope = new ManagementScope(@"[Name]\root\cimv2");
                scope.Connect();

                string wmiQuery = string.Format("Select * from Win32_Service", x.DisplayName);

                ManagementObjectSearcher wmi = new ManagementObjectSearcher(wmiQuery);
                ManagementObjectCollection coll = wmi.Get();

                foreach (var service in coll)
                {
                    Console.WriteLine(string.Format("{0} - {1}", service["Name"].ToString(), service["StartMode"].ToString()) );
                }
            }
        }

        Console.ReadKey();
    }

1 个答案:

答案 0 :(得分:0)

大部分情况下看起来不错。我将在您的计算机名称之前用“ \\”引导作用域。另外,如果您只是在寻找一种特定的服务,请在查询中添加WHERE子句。

public static void Main()
{
    string MachineName = "[Name]";

    var ctl = ServiceController.GetServices(MachineName);
    List<string> namelist = new List<string>();

    foreach (var x in ctl)
    {
        if (x.DisplayName == "NHS Card Checker")
        {
            Console.WriteLine(string.Format("NHS Card checker found on MPC - Status: {0}", x.Status));

            ConnectionOptions options = new ConnectionOptions();
            options.Impersonation = System.Management.ImpersonationLevel.Impersonate;
            ManagementScope scope = new ManagementScope(String.Format(@"\\{0}\root\cimv2", MachineName));
            scope.Connect();

            string wmiQuery = string.Format("Select * from Win32_Service WHERE DisplayName='{0}'" , x.DisplayName);

            ManagementObjectSearcher wmi = new ManagementObjectSearcher(wmiQuery);
            ManagementObjectCollection coll = wmi.Get();

            foreach (var service in coll)
            {
                Console.WriteLine(string.Format("{0} - {1}", service["Name"].ToString(), service["StartMode"].ToString()));
            }
        }
    }

    Console.ReadKey();
}

或者简化为仅使用WMI之类的...

string MachineName = "[Name]";
            string TargetService = "NHS Card Checker";

            {
                ConnectionOptions options = new ConnectionOptions();
                    options.Impersonation = System.Management.ImpersonationLevel.Impersonate;
                    ManagementScope scope = new ManagementScope(String.Format(@"\\{0}\root\cimv2", MachineName));
                    scope.Connect();

                    string wmiQuery = String.Format("Select * from Win32_Service WHERE DisplayName='{0}'", TargetService);

                    ManagementObjectSearcher wmi = new ManagementObjectSearcher(wmiQuery);
                    ManagementObjectCollection coll = wmi.Get();

                    if (coll.Count > 0)
                    {

                        foreach (var service in coll)
                        {
                            Console.WriteLine(string.Format("NHS Card checker found on MPC - Status: {0}", service["Status"].ToString()));
                            Console.WriteLine(string.Format("{0} - {1}", service["Name"].ToString(), service["StartMode"].ToString()));
                        }
                    }
                    else
                    {
                        Console.WriteLine(string.Format("{0} Service was not found", TargetService));
                    }

                }
            }

此外,在C#的较新实现中,您可以使用更容易的字符串插值方差。代替...

string.format("{0} is your value", VariableName");

您可以使用

$"{VariableName} is your value";