Using InstallUtil and silently setting a windows service logon username/password

后端 未结 5 1533
花落未央
花落未央 2020-12-22 23:29

I need to use InstallUtil to install a C# windows service. I need to set the service logon credentials (username and password). All of this needs to be done silently.

<
相关标签:
5条回答
  • 2020-12-23 00:08

    A much easier way than the posts above and with no extra code in your installer is to use the following:

    installUtil.exe /username=domain\username /password=password /unattended C:\My.exe

    Just ensure the account you use is valid. If not you will receive a "No mapping between account names and security id's was done" exception

    0 讨论(0)
  • 2020-12-23 00:13

    Bravo to my co-worker (Bruce Eddy). He found a way we can make this command-line call:

    installutil.exe /user=uname /password=pw myservice.exe
    

    It is done by overriding OnBeforeInstall in the installer class:

    namespace Test
    {
        [RunInstaller(true)]
        public class TestInstaller : Installer
        {
            private ServiceInstaller serviceInstaller;
            private ServiceProcessInstaller serviceProcessInstaller;
    
            public OregonDatabaseWinServiceInstaller()
            {
                serviceInstaller = new ServiceInstaller();
                serviceInstaller.StartType = System.ServiceProcess.ServiceStartMode.Automatic;
                serviceInstaller.ServiceName = "Test";
                serviceInstaller.DisplayName = "Test Service";
                serviceInstaller.Description = "Test";
                serviceInstaller.StartType = ServiceStartMode.Automatic;
                Installers.Add(serviceInstaller);
    
                serviceProcessInstaller = new ServiceProcessInstaller();
                serviceProcessInstaller.Account = ServiceAccount.User; 
                Installers.Add(serviceProcessInstaller);
            }
    
            public string GetContextParameter(string key)
            {
                string sValue = "";
                try
                {
                    sValue = this.Context.Parameters[key].ToString();
                }
                catch
                {
                    sValue = "";
                }
                return sValue;
            }
    
    
            // Override the 'OnBeforeInstall' method.
            protected override void OnBeforeInstall(IDictionary savedState)
            {
                base.OnBeforeInstall(savedState);
    
                string username = GetContextParameter("user").Trim();
                string password = GetContextParameter("password").Trim();
    
                if (username != "")
                    serviceProcessInstaller.Username = username;
                if (password != "")
                    serviceProcessInstaller.Password = password;
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-23 00:23

    No, installutil doesn't support that.

    Of course if you wrote an installer; with a custom action then you would be able to use that as part of an MSI or via installutil.

    0 讨论(0)
  • 2020-12-23 00:28

    InstallUtil.exe sets StartupType=Manual

    In case you want to autostart the service, use:

    sc config MyServiceName start= auto

    (note there there has to be a space after '=')

    0 讨论(0)
  • 2020-12-23 00:33

    You can also force your service to run as User using ServiceProcessInstaller::Account = ServiceAccount.User;

    A popup asking "[domain\]user, password" will appear during service installation.

    public class MyServiceInstaller : Installer
    {
        /// Public Constructor for WindowsServiceInstaller
        public MyServiceInstaller()
        {
            ServiceProcessInstaller serviceProcessInstaller = new ServiceProcessInstaller();
            ServiceInstaller serviceInstaller = new ServiceInstaller();
    
            //# Service Account Information
            serviceProcessInstaller.Account = ServiceAccount.User; // and not LocalSystem;
         ....
    
    0 讨论(0)
提交回复
热议问题