51

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
Craig Trader
  • 15,507
  • 6
  • 37
  • 55
Dean Hill
  • 4,369
  • 6
  • 31
  • 35

5 Answers5

73

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

Firo
  • 30,626
  • 4
  • 55
  • 94
Jimbo
  • 747
  • 5
  • 2
  • 11
    This only works if you set the Account property on ServiceProcessInstaller to 'ServiceAccount.User' – headsling Jun 17 '10 at 16:14
  • 5
    Also, to specify local machine use ".", like so: "/username=.\Administrator" – DenNukem May 01 '12 at 00:58
  • Note that using the well-known switch `password` shown here masks the password in the installation log file. – paulroho Feb 25 '20 at 14:52
  • @headsling If you're looking to specify username and password during install, I would assume you have the Service Account type set to user. – Chase Aug 10 '21 at 11:31
54

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;
        }
    }
}
Dean Hill
  • 4,369
  • 6
  • 31
  • 35
  • 6
    For anyone using this, make sure all arguments precede the ".exe" of the service on the command line, otherwise they are not processed/passed. – Ray Hayes Jun 28 '12 at 10:18
  • 4
    This will include the username / password in the installation logfile. Unless you disable the writing of logfiles this information will remain, which is quite dangerous I think. I haven't found a better solution yet :( – flayn Sep 14 '12 at 08:43
  • This even works with the ManagedInstallerClass ManagedInstallerClass.InstallHelper(new string[] { "/user=theUserName", "/password=******", Assembly.GetExecutingAssembly().Location }); – AUSTX_RJL Aug 05 '14 at 20:42
  • Command-line parameter is actually username not user – udog Jul 29 '21 at 16:59
4

InstallUtil.exe sets StartupType=Manual

In case you want to autostart the service, use:

sc config MyServiceName start= auto

(note there there has to be a space after '=')

Aaron D
  • 5,817
  • 1
  • 36
  • 51
Josua
  • 693
  • 1
  • 6
  • 3
  • In my case sc config MyServiceName start= auto not working – nikolai.serdiuk Jun 30 '16 at 09:26
  • 1
    This isn't entirely true. If you're creating your service from a .NET project, you can specify in the installer of the project what startup type to use. The installUtil utility respects it and gets installed with it. – Chase Aug 10 '21 at 11:33
0

No, installutil doesn't support that.

Of course if you wrote an installer; with a custom action then you would be able to use that as part of an MSI or via installutil.

blowdart
  • 55,577
  • 12
  • 114
  • 149
-2

You can also force your service to run as User using ServiceProcessInstaller::Account = ServiceAccount.User;

A popup asking "[domain\]user, password" will appear during service installation.

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;
     ....
william
  • 45
  • 1