Change NT Service user name and password after installation

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

Guest

Is there a way to change a NT Service's user name and password after it has
been installed. I want to perform a check for the user name in the On_Start
event of the service. If it is not the one I want I want to change the user
name and password and then allow the service to start.
 
I'd say that your service should launch an external app which will perform
the restart. Unfortunately if the user that your service is logged in as
doesn't have permission to start and stop services, and change service
permissions then this won't work.

Is there any particular reason you want to limit the user name? I would be
very unhappy if a service told me what user name it wanted. Surely all the
service really need is the correct permissions? Also, I'd classify any
service which elevated it's own privileges as a serious security risk.
Rather just fail on start and write to the event log what you want to
happen. Then the system administrator can decide what to do.

Also, if you're looking at changing to one of the standard user id's beware
about localization issues. Also keep in mind that many network admins remove
the standard user names off their systems. One I know has no Administrator
accounts on any machine.
 
The service has to run under a specific account that we specify to the
customer. There are a lot of reasons for this but would take too long to
explain. I need a programmatic way to modify the user name and password.
The service is a .NET service and I want to perform the check in the OnStart
event. If this is not possible, I do want to launch a .NET executable to
perform this task.

How do I do this programmatically through .NET?
 
Bruce Parker said:
Is there a way to change a NT Service's user name and password after it
has
been installed. I want to perform a check for the user name in the
On_Start
event of the service. If it is not the one I want I want to change the
user
name and password and then allow the service to start.

FWIW:

I don't know enough about .Net to know what your options are there.

If you find that it doesn't have support for what you want to do, and if you
are comfortable operating at the level of the Win32 API then you can use
OpenSCManager(), OpenService(), ChangeServiceConfig() to alter an installed
service's parameters. If you go that route, don't forget to close any
handles that you open.

Regards,
Will
 
From the looks of things you'll have to use P/Invoke for this. The
ServiceController doesn't have a Restart method, nor does it provide access
to the user credentials. You could get around the Restart issue by spawning
another process that starts the service (say, "net start myservice") on
service stop.
 
Back
Top