none
How do I find out the ServiceAccount type of an existing service? (LocalService, NetworkService, LocalSystem, User) RRS feed

  • Question

  • I've looked over the ServiceContoler class. This does a great job of starting and stopping the services and getting the state of the services.

    I would like to find the other properties of an existing service: ServiceAccount and ServiceStartMode. If the server is using an account, then the name of the account.

    I've review the ServiceInstaller, but this only seems to work for installing a service.

     


    Brad Thompson
    Friday, December 17, 2010 1:56 AM

Answers

  • After looking around, I discovered that the WMI is the only way to do this. Thankfully, .Net Framework 3.5 has given a nicer way to call into the WMI. Here is the code that will do what I needed:

    using System;
    using System.IO;
    using System.Management;
    
    public class ServiceQuery
    {
      public static void MsSqlServer(string serverName)
      {
        ConnectionOptions options = new ConnectionOptions();
        string path = string.Format(@"\\{0}\root\cimv2", serverName);
        ManagementScope scope = new ManagementScope(path, options);
        scope.Connect();
        ObjectQuery query = new ObjectQuery("SELECT * FROM Win32_Service");
        ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);
        ManagementObjectCollection queryCollection = searcher.Get();
        foreach (ManagementObject m in queryCollection)
        {
          string serviceName = m.Properties["Name"].Value.ToString();
          if (serviceName.Equals("MsSqlServer", StringComparison.OrdinalIgnoreCase))
          {
            Console.WriteLine("{0,-12}: {1}", "Caption:", m.Properties["Caption"].Value);
            Console.WriteLine("{0,-12}: {1}", "Description", m.Properties["Description"].Value);
            Console.WriteLine("{0,-12}: {1}", "DisplayName", m.Properties["DisplayName"].Value);
            Console.WriteLine("{0,-12}: {1}", "Name", m.Properties["Name"].Value);
            Console.WriteLine("{0,-12}: {1}", "PathName", m.Properties["PathName"].Value);
            Console.WriteLine("{0,-12}: {1}", "Started", m.Properties["Started"].Value);
            Console.WriteLine("{0,-12}: {1}", "StartMode", m.Properties["StartMode"].Value);
            Console.WriteLine("{0,-12}: {1}", "StartName", m.Properties["StartName"].Value);
            Console.WriteLine("{0,-12}: {1}", "State", m.Properties["State"].Value);
            Console.WriteLine("{0,-12}: {1}", "Status", m.Properties["Status"].Value);
            Console.WriteLine();
          }
        }
      }
    }
    
    

     


    Brad Thompson
    • Proposed as answer by eryang Saturday, December 18, 2010 2:51 AM
    • Marked as answer by eryang Friday, December 24, 2010 4:15 AM
    Friday, December 17, 2010 8:16 PM

All replies

  • After looking around, I discovered that the WMI is the only way to do this. Thankfully, .Net Framework 3.5 has given a nicer way to call into the WMI. Here is the code that will do what I needed:

    using System;
    using System.IO;
    using System.Management;
    
    public class ServiceQuery
    {
      public static void MsSqlServer(string serverName)
      {
        ConnectionOptions options = new ConnectionOptions();
        string path = string.Format(@"\\{0}\root\cimv2", serverName);
        ManagementScope scope = new ManagementScope(path, options);
        scope.Connect();
        ObjectQuery query = new ObjectQuery("SELECT * FROM Win32_Service");
        ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);
        ManagementObjectCollection queryCollection = searcher.Get();
        foreach (ManagementObject m in queryCollection)
        {
          string serviceName = m.Properties["Name"].Value.ToString();
          if (serviceName.Equals("MsSqlServer", StringComparison.OrdinalIgnoreCase))
          {
            Console.WriteLine("{0,-12}: {1}", "Caption:", m.Properties["Caption"].Value);
            Console.WriteLine("{0,-12}: {1}", "Description", m.Properties["Description"].Value);
            Console.WriteLine("{0,-12}: {1}", "DisplayName", m.Properties["DisplayName"].Value);
            Console.WriteLine("{0,-12}: {1}", "Name", m.Properties["Name"].Value);
            Console.WriteLine("{0,-12}: {1}", "PathName", m.Properties["PathName"].Value);
            Console.WriteLine("{0,-12}: {1}", "Started", m.Properties["Started"].Value);
            Console.WriteLine("{0,-12}: {1}", "StartMode", m.Properties["StartMode"].Value);
            Console.WriteLine("{0,-12}: {1}", "StartName", m.Properties["StartName"].Value);
            Console.WriteLine("{0,-12}: {1}", "State", m.Properties["State"].Value);
            Console.WriteLine("{0,-12}: {1}", "Status", m.Properties["Status"].Value);
            Console.WriteLine();
          }
        }
      }
    }
    
    

     


    Brad Thompson
    • Proposed as answer by eryang Saturday, December 18, 2010 2:51 AM
    • Marked as answer by eryang Friday, December 24, 2010 4:15 AM
    Friday, December 17, 2010 8:16 PM
  • Nice finding! Brad.
    Eric Yang [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Saturday, December 18, 2010 2:51 AM