2008-09-26 192 views

回答

51

布拉沃给我的同事(布鲁斯艾迪)。他找到了一种方法,我们可以使此命令行电话:使用

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

对于任何使用此操作的人,请确保所有参数都位于命令行上服务的“.exe”之前,否则它们不会被处理/传递。 – 2012-06-28 10:18:48

+2

这将在安装日志文件中包含用户名/密码。除非您禁用日志文件的写入,否则这些信息将保留,这是非常危险的,我认为。我还没有找到更好的解决方案:( – flayn 2012-09-14 08:43:03

+0

这甚至可以与ManagedInstallerClass ManagedInstallerClass.InstallHelper(新字符串[] {“/ user = theUserName”,“/ password = ******”,程序集。 GetExecutingAssembly()。Location}); – 2014-08-05 20:42:45

3

不,installutil不支持。

当然,如果你写了一个安装程序;与custom action然后你将能够使用它作为MSI的一部分或通过installutil。

2

您还可以强制为您服务,为用户运行:

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

它是由安装程序类中重写OnBeforeInstall完成ServiceProcessInstaller :: Account = ServiceAccount.User;

在服务安装过程中会弹出一个询问“[domain \] user,password”的弹出窗口。

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; 
    .... 
+3

-1该问题明确要求进行无提示安装 – 2014-03-12 11:21:31

61

比上面的帖子,并与您的安装没有额外的代码更简单的方法是使用以下命令:

installUtil.exe /用户名=域\用户名/密码=密码/无人值守C:\ My.exe

只要确保您使用的帐户有效。如果没有,你会收到一个“帐户名和安全的ID之间没有映射已完成”异常

5

InstallUtil.exe设置StartupType =手动

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

sc config MyServiceName start= auto

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

相关问题