使用InstallUtil并静默设置Windows服务登录用户名/密码 [英] Using InstallUtil and silently setting a windows service logon username/password

查看:413
本文介绍了使用InstallUtil并静默设置Windows服务登录用户名/密码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用InstallUtil来安装C#Windows服务。我需要设置服务登录凭据(用户名和密码)。所有这些都需要静默地完成。

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.

是否可以执行以下操作:

Is there are way to do something like this:

installutil.exe myservice.exe /customarg1=username /customarg2=password


推荐答案

Bravo给我的同事(Bruce Eddy)。他找到了可以进行此命令行调用的方法:

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

这是通过在安装程序类中覆盖OnBeforeInstall来完成的:

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;
        }
    }
}

这篇关于使用InstallUtil并静默设置Windows服务登录用户名/密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆