Find which account a service is set to “Log On As”

后端 未结 3 1284
悲&欢浪女
悲&欢浪女 2021-01-17 23:23

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

3条回答
  •  攒了一身酷
    2021-01-17 23:36

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

提交回复
热议问题