When installing my Windows service, how can I programmatically make it use a specific domain account?

I have a windows service that is running fine, but I have to run it under a special user account.

I am currently going to services and changing the login as a section, but it needs to be done more professionally for deployment.

Is there a way so that I can log into it as a user account with the software or during the service installation process?

+1


source to share


4 answers


When you open the COntrol Service Manager (SCM), of course, there is a tab labeled Login. There you can specify which domain or computer account it should run under ...

But programmatically. if you use the Service Installer class in your code you can specify it there.



 public class MyServiceInstaller : Installer
    {
        private ServiceInstaller servInst;
        private ServiceProcessInstaller servProcInst;
        public MyServiceInstaller () { InitializeComponent(); }

        #region Component Designer generated code
        private void InitializeComponent()
        {
            servInst = new ServiceInstaller();
            servProcInst = new ServiceProcessInstaller();
            // -----------------------------------------------
            servProcInst.Account = ServiceAccount.LocalSystem; // or whatever accnt you want
            servProcInst.Username = null;  // or, specify a specifc acct here
            servProcInst.Password = null;
            servProcInst.AfterInstall += 
                new InstallEventHandler(this.AfterServProcInstall);
            servInst.ServiceName = "MyService";
            servInst.DisplayName = "Display name for MyService";
            servInst.Description = " Description for my service";
            servInst.StartType = ServiceStartMode.Automatic;
            servInst.AfterInstall += 
               new InstallEventHandler(this.AfterServiceInstall);
            Installers.AddRange(new Installer[] { servProcInst, servInst });
        }
        #endregion
    }
    private void AfterServiceInstall(object sender, InstallEventArgs e) { }
    private void AfterServProcInstall(object sender, InstallEventArgs e) { }

      

+4


source


Look at the CreateService function , especially in the argument lpServiceStartName

. This is the "name of the account under which the service should run".



+1


source


How do you install? Is it a .net service (in this case, I think you can specify the account in the installer object).

Typically, installation technology allows you to change credentials (except perhaps registering a COM service)

If you register xcopy and then register on first run, you can use ChangeServiceConfig (..., ServiceName, Password, ...) to correct the registration.

0


source


Came through this and thought I chose the Powershell option as well, since it's convenient for me and it might help someone:

$svc = gwmi win32_service -computername <computer name> -filter "name='<name of your service>'"
$inParams = $svc.psbase.getMethodParameters("change")
$inParams["StartName"] = '<domain\username>'
$inParams["StartPassword"] = '<password>'
$null = $svc.invokeMethod("change", $inParams, $null)

      

0


source







All Articles