11

I am installing a Windows service using WiX. How can I make the service run in the context of Windows User that runs the installer?

kjv
  • 11,047
  • 34
  • 101
  • 140
  • Why would you want to run the service as the current user? Much of the point of a service is to be able to run when there is no user logged on - and to be able to run as LocalSystem to facilitate admin rights for the executable. I am sure you have a good reason... – Stein Åsmul Oct 02 '09 at 04:26
  • 2
    One reason why you would run it as a particular user is that it needs a connection to the SQL server and SQL server is configured to use Windows Authentication... it's easier to not have to add the "Local Service" to the list of SQL Users. – Richard B Sep 07 '10 at 08:37

1 Answers1

26

You need to have both the account name and password for the user you want to run the service as. I was able to accomplish this by adding a custom UI to my installer asking for a User Name and Password, and then using the supplied values for the Account and Password attributes on the ServiceInsall element.

Note that what ever account is used to run the service will need to have the Log On As Service privileged. This is not granted to users by default. I was able to use the User element from the UtilExtension schema to add this priveledge to the user. Adding the privileged to the user would only succeed if the user running the installer is an administrator.

Here's the code I used. SERVICECREDENTIALS_USERLOGIN and SERVICECREDENTIALS_PASSWORD are the properties populated from the custom UI.

<Component Id="ServiceEXE" Guid="xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx">
  <File Id="ServiceEXE" Name="YourService.exe" DiskId="1"
        Source="path\to\YourService.exe" KeyPath="yes" />
  <util:User Id="UpdateUserLogonAsService" UpdateIfExists="yes" CreateUser="no" Name="[SERVICECREDENTIALS_USERLOGIN]"
             LogonAsService="yes" />
  <ServiceInstall Id="ServiceInstall" Type="ownProcess" Vital="yes" Name="YourService"
                  DisplayName="Your Service" Description="Your Service description"
                  Start="auto" Account="[SERVICECREDENTIALS_USERLOGIN]" Password="[SERVICECREDENTIALS_PASSWORD]"
                  ErrorControl="normal" Interactive="no" />
  <ServiceControl Id="StartService" Start="install" Stop="both" Remove="uninstall" Name="YourService" Wait="yes" />
</Component>
jhs
  • 276
  • 3
  • 2
  • can you help me how can i have 2 components 1 for SQL Authentication and 1 for Windows authentication i.e. how to add condition. – Sunil Agarwal Sep 14 '11 at 10:25
  • Does this code work at all? setting the LogonAsService privilege *and* the service identity does not seem to be scheduled sequentially in the resulting .msi package. For this reason, installation fails... Or am I missing something? – Maxime Labelle Oct 17 '12 at 15:36