Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 
like image 391
Dean Hill Avatar asked Sep 26 '08 15:09

Dean Hill


People also ask

What is the use of Installutil EXE?

Installutil.exe detects and executes these installer components. You can specify multiple assemblies on the same command line. Any option that occurs before an assembly name applies to that assembly's installation. Except for /u and /AssemblyName , options are cumulative but overridable.


2 Answers

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

like image 121
Jimbo Avatar answered Sep 24 '22 18:09

Jimbo


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;         }     } } 
like image 39
Dean Hill Avatar answered Sep 23 '22 18:09

Dean Hill