0

I'm trying to identify if a certain service on a remote PC is running or not and identify its start up type.

Using ServiceController I can successfully see the expected service on the remote machine but when switching to use WMI to drill deeper this service no longer appears.

Heres my code:

    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();
    }
Ben R
  • 85
  • 6

1 Answers1

0

Looks good for the most part. I would lead the scope with "\\" before your machine name. Also, if you are just looking for one specific service, add a WHERE clause to your query.

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();
}

or maybe simplify it to only use WMI like...

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));
                    }

                }
            }

Also, in newer implementations of C# you can use an easier variance of string interpolation. Instead of...

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

you can use

$"{VariableName} is your value";
Paul G
  • 1,219
  • 7
  • 14