Q:

C# Get service log on as using ServiceController class?

Is it possible to get the logon as account using the ServiceController class?  I am using the following code to get the service display names and the service status on a remote machine.  I do not see a property indicating what account the service is running under. If not then is there another class that I can use to find out what account a service is running under?
ServiceController[] services = ServiceController.GetServices("MyRemotePC");

foreach (ServiceController service in services)
{                                     
    Console.WriteLine(
        "The {0} service is currently {1}.",
        service.DisplayName,
        service. Status
        );
}                

A:

For each service the following first checks to see if the service is running. 
 If so, it gets the service's processId and uses ManagementObjectSearch to retrieve the corresponding process object.  From there it calls GetOwner(out string user, out string domain) from the underlying Win32_Process object, and outputs the result if the call was successful.
The code below worked locally, however I don't have the access to test this against a remote computer.  Even locally I had to run the application as an administrator.  for GetOwner to not return an error result of 2 (Access Denied). 
var services = ServiceController.GetServices("MyRemotePC");
var getOptions = new ObjectGetOptions(null, TimeSpan.MaxValue, true);
var scope = new ManagementScope(@"\\MyRemotePC\root\cimv2");

foreach (ServiceController service in services)
{
    Console.WriteLine($"The {service.DisplayName} service is currently {service.Status}.");

    if (service.Status != ServiceControllerStatus.Stopped)
    {
        var svcObj = new ManagementObject(scope, new ManagementPath($"Win32_Service.Name='{service.ServiceName}'"), getOptions);
        var processId = (uint)svcObj["ProcessID"];
        var searcher = new ManagementObjectSearcher(scope, new SelectQuery($"SELECT * FROM Win32_Process WHERE ProcessID = '{processId}'"));
        var processObj = searcher.Get().Cast<ManagementObject>().First();
        var props = processObj.Properties.Cast<PropertyData>().ToDictionary(x => x.Name, x => x.Value);
        string[] outArgs = new string[] { string.Empty, string.Empty };
        var returnVal = (UInt32)processObj.InvokeMethod("GetOwner", outArgs);
        if (returnVal == 0)
        {
            var userName = outArgs[1] + "\\" + outArgs[0];
            Console.WriteLine(userName);
        }
    }
}

