我正在使用 WiX 安装 Windows 服务。如何使服务在运行安装程序的 Windows 用户的上下文中运行?
最佳答案
您需要拥有要作为其运行服务的用户的帐户名和密码。我能够通过向我的安装程序添加自定义 UI 来要求用户名和密码,然后使用 ServiceInsall 元素上的 Account 和 Password 属性提供的值来完成此操作。
请注意,用于运行服务的任何帐户都需要具有登录身份服务特权。默认情况下,这不会授予用户。我能够使用 UtilExtension 架构中的 User 元素将此权限添加到用户。仅当运行安装程序的用户是管理员时,才能向用户添加特权。
这是我使用的代码。 SERVICECREDENTIALS_USERLOGIN 和 SERVICECREDENTIALS_PASSWORD 是从自定义 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>
关于WiX ServiceInstall - 设置服务以当前 Windows 用户身份运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1487638/