Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I retrieve the username that a Windows service is running under?

Given a service name, I would like to retrieve the username that it runs under (i.e. the username shown in the 'Log On' tab of a service's properties window).

There doesn't appear to be anything in the ServiceController class to retrieve this basic information. Nothing else in System.ServiceProcess looks like it exposes this information either.

Is there a managed solution to this, or am I going to have to drop down into something lower-level?

like image 571
Mark Carpenter Avatar asked Jun 29 '10 13:06

Mark Carpenter


People also ask

How do I find my Windows service name?

WindowsIdentity. GetCurrent(). Name; But, it is giving the output "NT AUTHORITY\SYSTEM " as the current user name after deploying the service.

What user do Windows services run as?

The default user account on Windows under which services install is the "Local System" account.

What is a service login?

The Log on as a service user right allows accounts to start network services or services that run continuously on a computer, even when no one is logged on to the console. The risk is reduced because only users who have administrative privileges can install and configure services.

Can two Windows services have the same name?

Answer. No, you should not have 2 services with the same name. It is mentioned in the Service Configuration Guide that service names should be unique. It it may be possible to create 2 services with the same name, but this can lead to problems in the future and is not a supported configuration.


2 Answers

WMI is your friend. Look at Win32_Service, specifically the StartName property. You can access WMI from C# via the System.Management.ManagementClass.

If you've not used WMI before, this article seems to be quite a good tutorial.

like image 45
Hans Olsson Avatar answered Sep 28 '22 08:09

Hans Olsson


Using WMI, with the System.Management you can try the following code:

using System;
namespace WindowsServiceTest
{
    class Program
    {
        static void Main(string[] args)
        {
            System.Management.SelectQuery sQuery = new System.Management.SelectQuery(string.Format("select name, startname from Win32_Service")); // where name = '{0}'", "MCShield.exe"));
            using (System.Management.ManagementObjectSearcher mgmtSearcher  = new System.Management.ManagementObjectSearcher(sQuery))
            {
                foreach (System.Management.ManagementObject service in mgmtSearcher.Get())
                {
                    string servicelogondetails =
                        string.Format("Name: {0} ,  Logon : {1} ", service["Name"].ToString(), service["startname"]).ToString();
                    Console.WriteLine(servicelogondetails);
                }
            }
            Console.ReadLine();
        }
    }
}

You can then later substitute the commented code with your service name, and it should only return the instances of your service process that is running.

like image 192
Riaan Avatar answered Sep 28 '22 08:09

Riaan