当前位置:  开发笔记 > 编程语言 > 正文

使用InstallUtil并静默设置Windows服务登录用户名/密码

如何解决《使用InstallUtil并静默设置Windows服务登录用户名/密码》经验,为你挑选了3个好方法。

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

有没有办法做这样的事情:

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

小智.. 70

比上面的帖子更简单的方法,安装程序中没有额外的代码是使用以下内容:

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

只需确保您使用的帐户有效.如果没有,您将收到"帐户名称与安全ID之间没有映射"的例外情况



1> 小智..:

比上面的帖子更简单的方法,安装程序中没有额外的代码是使用以下内容:

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

只需确保您使用的帐户有效.如果没有,您将收到"帐户名称与安全ID之间没有映射"的例外情况


这仅适用于将ServiceProcessInstaller上的Account属性设置为"ServiceAccount.User"的情况
另外,要指定本地机器使用".",如下所示:"/ username=.\Administrator"

2> Dean Hill..:

布拉沃给我的同事(布鲁斯艾迪).他找到了我们可以进行此命令行调用的方法:

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

它是通过在安装程序类中重写OnBeforeInstall来完成的:

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


对于使用此方法的任何人,请确保所有参数都在命令行上的服务的".exe"之前,否则它们不会被处理/传递.
这将包括安装日志文件中的用户名/密码.除非您禁用日志文件的写入,否则此信息将保留,我认为这非常危险.我还没有找到更好的解决方案:(

3> 小智..:

InstallUtil.exe 设置StartupType = Manual

如果您想自动启动该服务,请使用:

sc config MyServiceName start= auto

(注意'='之后必须有空格)

推荐阅读
oDavid_仔o_880
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有