问题
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?
回答1:
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>
来源:https://stackoverflow.com/questions/1487638/wix-serviceinstall-setting-the-service-to-run-as-the-current-windows-user