Retrieving account starting service

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I’m trying to write an application to enumerate all the services running on
all of the servers in the company I work for. I’ve got it working except that
I have not been able find how to pull the account that starts the service.
This is important because once I know this info for all the servers, I have
to write an app that will periodically change the passwords used by the
accounts starting these services. I don’t want to logon to all of these
servers individually to do this. I’ve been able to get the information from
many of the servers by pulling the info from the registry, but many of the
servers have remote registry disabled.
 
You should use the service controller manager to perform all services
related queries/changes.
Look at the System.ServiceProcess.ServiceController Class for a start.

/LM
 
That's what I'm using now, but I can not find how to access what account is
starting the services. The ServiceController returns everything else, but not
the account that starts the service.

Bill
 
Damn, you are right. Although I wrote services in .NET, I still use the
management tools (in plain C) I wrote for the SCM at the time of the first
beta of NT circa 1991/1992...
I see the ServiceInstaller has the Account/Username/Password info, but no
way to connect that to an existing service.

Let's hope someone else come on with a pure .NET solution (if it is
possible). You can of course use the SCM interface with PInvoke.

/LM
 
Bill Richards said:
I’m trying to write an application to enumerate all the services running
on
all of the servers in the company I work for. I’ve got it working except
that
I have not been able find how to pull the account that starts the service.
This is important because once I know this info for all the servers, I
have
to write an app that will periodically change the passwords used by the
accounts starting these services. I don’t want to logon to all of these
servers individually to do this. I’ve been able to get the information
from
many of the servers by pulling the info from the registry, but many of the
servers have remote registry disabled.

Using System.Management (WMI wrappers) is the best way to manage services
and processes.

Here is a sample that retrieves the identity of the alerter service.

....
string name = "alerter";
string CIMObject = String.Format("win32_Service.Name='{0}'", name);
using(ManagementObject mo = new ManagementObject(CIMObject))
{
mo.Get();
string StartName = mo["StartName"].ToString();
Console.WriteLine(StartName);
}

Changing the identity and password can be done by invoking the "Change"
method.

ManagementBaseObject inParams = null;
inParams = srvc.GetMethodParameters("Change");
inParams["StartName"] = Name;
inParams["StartPassword"] = Password;
ManagementBaseObject outParams = srvc.InvokeMethod("Change",
inParams, null);
int ret =
System.Convert.ToInt32(outParams.Properties["ReturnValue"].Value);

For detailed info on WMI classes and methods check MSDN.

Willy.
 
Back
Top