2008-09-26 9 views

Odpowiedz

51

Bravo dla mojego współpracownika (Bruce Eddy). Znalazł sposób możemy dokonać tego połączenia wiersza poleceń:

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

Stało się nadrzędnymi OnBeforeInstall w klasie instalatora:

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

Dla każdego, kto tego używa, upewnij się, że wszystkie argumenty poprzedzają ".exe" usługi w wierszu poleceń, w przeciwnym razie nie są przetwarzane/przekazywane. –

+2

Dotyczy to nazwy użytkownika/hasła w pliku dziennika instalacji. Dopóki nie wyłączysz zapisywania logów, te informacje pozostaną, co jest dość niebezpieczne. Nie znalazłem jeszcze lepszego rozwiązania :( – flayn

+0

Działa to nawet z ManagedInstallerClass ManagedInstallerClass.InstallHelper (nowy ciąg [] {"/ user = theUserName", "/ password = ******", Assembly. GetExecutingAssembly(). Lokalizacja}); –

3

Nie, installutil nie obsługuje tego.

Oczywiście, jeśli napisałeś instalator; z custom action, będziesz mógł użyć tego jako części MSI lub poprzez installutil.

2

Można także wymusić swoją usługę do uruchomienia jako Użytkownik korzystający ServiceProcessInstaller :: Account = ServiceAccount.User;

Pojawi się okienko z pytaniem "[domena \] użytkownik, hasło" podczas instalacji usługi.

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 pytanie jednoznacznie prosi o cichą instalację. –

61

znacznie łatwiejszy sposób niż słupki powyżej bez dodatkowego kodu w instalatorze jest użycie następujących:

installUtil.exe/username = domena \ nazwa użytkownika/hasło = hasło/bez nadzoru C: \ My.exe

Upewnij się, że używane konto jest prawidłowe. Jeśli nie otrzymasz „No mapowanie między nazwami kont i identyfikator Security zostało zrobione” wyjątek

+10

Działa to tylko wtedy, gdy ustawisz właściwość Konto na ServiceProcessInstaller na "ServiceAccount.User" – headsling

+4

Również, aby określić lokalne użycie komputera ".", Np. so: "/username=.\Administrator" – DenNukem

5

InstallUtil.exe ustawia StartupType = Ręczne

W przypadku, gdy chcesz autostartu usługi, przeznaczenie:

sc config MyServiceName start= auto

(zauważ, że musi być spacja po '=')

+0

W moim przypadku sc config MyServiceName start = auto nie działa –

Powiązane problemy