read serviceaccount of a windows serice

  • Thread starter Thread starter pit_the_kid
  • Start date Start date
P

pit_the_kid

Hello,

I want to get the serviceaccount of an already installed windows
service. With the class Service.Controller (under
System.ServiceProcess) I can read a couple of useful properties of
existing services, but there is no way to read the account under which
the service runs.
On the other hand there is the class ServiceProcessInstaller which
gives for the Installation of Services the possibilty to specifiy
servicetype and in case of user the serviceaccount.
Can I use this class to read the serviceaccounts/-types of existing
services. If yes, how do I do this? If this class is not suitable for
my intention, which alternative classes can I use?

Thx in advance

Pit
 
Hello,

I want to get the serviceaccount of an already installed windows
service. With the class Service.Controller (under
System.ServiceProcess) I can read a couple of useful properties of
existing services, but there is no way to read the account under which
the service runs.
On the other hand there is the class ServiceProcessInstaller which
gives for the Installation of Services the possibilty to specifiy
servicetype and in case of user the serviceaccount.
Can I use this class to read the serviceaccounts/-types of existing
services. If yes, how do I do this? If this class is not suitable for
my intention, which alternative classes can I use?

Thx in advance

Pit


You can use the System.Management namespace classes for this, following
snippet shows you how to get the service name and the account he's running
in. Search MSDN for details on the WMI class "Win32_Service" property
descriptions.

....
SelectQuery query = new SelectQuery("select name, startname from
Win32_Service where name='Eventlog'");
using(ManagementObjectSearcher searcher = new
ManagementObjectSearcher(query))
{
foreach (ManagementObject service in searcher.Get()) {
Console.WriteLine("Name: {0} - Logon : {1} ", service["Name"],
service["startname"]);
}
}
....

Willy.
 
Back
Top