Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find which account a service is set to "Log On As"

How to find out the user account (Local System/User etc) a service is set to run under ("Log On As")?

Unlike this similar question this code can't run from within the service itself and the service may not be running.

The System.ServiceProcess.ServiceController class has useful methods for getting the status but not the "Log On As" user.

like image 222
Ryan Avatar asked Oct 27 '09 16:10

Ryan


2 Answers

This is the only way I know of, I found it looking around and tested it, it works. Make sure you use the Service Name not it's Display Name, you will also need to add a reference to System.Management

string serviceName = "aspnet_state";

SelectQuery query = new System.Management.SelectQuery(string.Format(
    "select name, startname from Win32_Service where name = '{0}'", serviceName));
using (ManagementObjectSearcher searcher =
    new System.Management.ManagementObjectSearcher(query))
{
    foreach (ManagementObject service in searcher.Get())
    {
        Console.WriteLine(string.Format(
            "Name: {0} - Logon : {1} ", service["Name"], service["startname"]));
    }
}
like image 200
Stan R. Avatar answered Oct 31 '22 11:10

Stan R.


How about using WMI and the Win32_Service class with the StartName parameter?

This article might help.

like image 25
serialhobbyist Avatar answered Oct 31 '22 10:10

serialhobbyist