我可以使用 Wix InstallService 设置 Windows 服务的恢复选项吗?如果可以,如何设置?我找不到任何用于传递恢复选项设置的参数。
或者,在 WiX 安装程序完成安装后,如何运行 sc.exe 来设置服务恢复选项。
我在其他地方找到了这个 Wix 代码,但它似乎什么也没做。到底应该在 WiX package.wsx 的哪个位置来确保它被调用?
<CustomAction Id="SetRecoveryOptions"
Directory="INSTALLFOLDER"
Execute="immediate"
Impersonate="no"
ExeCommand="cmd.exe /c "sc failure MyWindowsPService reset= 0 actions= restart/60000/restart/60000/restart/60000""
Return="asyncNoWait" />
<InstallExecuteSequence>
<Custom Action="SetRecoveryOptions"
After="InstallFinalize" />
</InstallExecuteSequence>
完整的 Wix 包文件
<?xml version="1.0" encoding="UTF-8"?>
<!-- Define the variables in "$(var.*) expressions" -->
<?define Name = "My Windows Service" ?>
<?define Manufacturer = "XYZ Pty Ltd" ?>
<?define Version = "1.0.1.0" ?>
<?define UpgradeCode = "9ED3FF33-8718-444E-B44B-69A34433B7E98" ?>
<Wix xmlns="http://wixtoolset.org/schemas/v4/wxs">
<Package Name="$(Name)"
Manufacturer="$(Manufacturer)"
Version="$(Version)"
UpgradeCode="$(var.UpgradeCode)"
Compressed="true">
<!-- Allow upgrades and prevent downgrades -->
<MajorUpgrade DowngradeErrorMessage="A later version of [ProductName] is already installed. Setup will now exit." />
<!-- Define the directory structure -->
<Directory Id="TARGETDIR" Name="SourceDir">
<Directory Id="ProgramFiles64Folder">
<!-- Create a folder inside program files -->
<Directory Id="ROOTDIRECTORY" Name="$(var.Manufacturer)">
<!-- Create a folder within the parent folder given the name -->
<Directory Id="INSTALLFOLDER_" Name="$(Name)" />
</Directory>
</Directory>
</Directory>
<MediaTemplate EmbedCab="yes" />
<ComponentGroup Id="ProgramFiles" Directory="INSTALLFOLDER_">
<Component Id="ABC.dll" Guid="*">
<File Id="ABC.dll" KeyPath="true" Source="$(var.MyWindowsServices.TargetDir)\ABC.dll" />
</Component>
...
</ComponentGroup>
<!-- The files inside this DirectoryRef are linked to
the App.MyWindowsService directory via INSTALLFOLDER -->
<DirectoryRef Id="INSTALLFOLDER_">
<!-- Create a single component which is the App.MyWindowsService.exe file -->
<Component Id="ServiceExecutable" Bitness="always64">
<!-- Copies the MyWindowsServices.exe file using the
project reference preprocessor variables -->
<File Id="MyWindowsServices.exe"
Source="$(var.MyWindowsServices.TargetDir)\MyWindowsServices.exe"
KeyPath="true" />
<!-- Remove all files from the INSTALLFOLDER on uninstall -->
<RemoveFile Id="ALLFILES" Name="*.*" On="both" />
<!-- Tell WiX to install the Service -->
<ServiceInstall Id="ServiceInstaller"
Type="ownProcess"
Name="MyWindowsService"
DisplayName="$(Name)"
Description="My Windows Service"
Account=".\LocalSystem"
Start="auto"
ErrorControl="normal" />
<!-- Tell WiX to start the Service -->
<ServiceControl Id="StartService"
Start="install"
Stop="both"
Remove="uninstall"
Name="MyWIndowsServices"
Wait="true" />
</Component>
</DirectoryRef>
<CustomAction Id="SetRecoveryOptions"
Directory="INSTALLFOLDER"
Execute="immediate"
Impersonate="no"
ExeCommand="cmd.exe /c "sc failure MyWindowsServices reset= 0 actions= restart/60000/restart/60000/restart/60000""
Return="asyncNoWait" />
<InstallExecuteSequence>
<Custom Action="SetRecoveryOptions"
After="InstallFinalize" />
</InstallExecuteSequence>
<!-- Tell WiX to install the files -->
<Feature Id="Service" Title="MyWindowsServices Setup" Level="1">
<ComponentRef Id="ServiceExecutable" />
<ComponentGroupRef Id="ProgramFiles" />
</Feature>
</Package>
</Wix>
好吧,看来我只需要删除 cmd.exe 并直接运行 sc.exe 即可。
...
<CustomAction Id="SetRecoveryOptions"
Directory="INSTALLFOLDER_"
Execute="immediate"
Impersonate="no"
ExeCommand="[SystemFolder]sc.exe failure MyWindowsService reset= 0 actions= restart/60000/restart/60000/restart/60000"
Return="ignore" />
<!-- Tell WiX to install the files -->
<Feature Id="Service" Title="MyWindowsServices Setup" Level="1">
<ComponentRef Id="ServiceExecutable" />
<ComponentGroupRef Id="Azure" />
</Feature>
<InstallExecuteSequence>
<Custom Action="SetRecoveryOptions"
Before="InstallFinalize" />
</InstallExecuteSequence>
</Package>
</Wix>