WiX is used to create installers. This tool is integrated into visual studio and can be used in any build process or for automatic deployment. No matter how good you software is, if you don’t have a good release/deployment process, you will reduce your client satisfaction and have extra costs for each deployment.
This is a simple example of a WiX installer which copies all selected files to a defined folder and then executes a simple power shell script. When uninstalling, a second script is then executed.
First a simple console application (our software project to be deployed) is required. For simplicity, the 2 power shell scripts are included in this deployment. The project is deployed using a simple harvest. For the power shell script, a custom action is used.
<!-- My custom actions install scripts--> <CustomAction Id="RegisterPSCmd" Property="RegisterPowerShellProperty" Value=""C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -NoLogo -NonInteractive -InputFormat None -NoProfile -File "C:\damienbod\SimpleConsoleApp\CreateDirectoryAndFilesInstall.ps1" "arg_that_I_maybe_require_for_my_script"" Execute="immediate" /> <CustomAction Id="RegisterPSCmdUninstall" Property="RegisterPowerShellPropertyUninstall" Value=""C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -NoLogo -NonInteractive -InputFormat None -NoProfile -File "C:\damienbod\SimpleConsoleApp\CreateDirectoryAndFilesUninstall.ps1" "arg_that_I_maybe_require_for_my_unistallscript"" Execute="immediate" /> <CustomAction Id="RegisterPowerShellProperty" BinaryKey="WixCA" DllEntry="CAQuietExec64" Execute="deferred" Return="check" Impersonate="no" /> <CustomAction Id="RegisterPowerShellPropertyUninstall" BinaryKey="WixCA" DllEntry="CAQuietExec64" Execute="deferred" Return="check" Impersonate="no" />
The custom actions are added to the InstallExecuteSequence.
<InstallExecuteSequence> <Custom Action="RegisterPSCmd" After="CostFinalize">NOT Installed</Custom> <Custom Action="RegisterPowerShellProperty" After="InstallFiles">NOT Installed</Custom> <Custom Action='RegisterPSCmdUninstall' Before='CostInitialize'>(NOT UPGRADINGPRODUCTCODE) AND (REMOVE="ALL")</Custom> <Custom Action='RegisterPowerShellPropertyUninstall' Before='RemoveFiles'>(NOT UPGRADINGPRODUCTCODE) AND (REMOVE="ALL")</Custom> </InstallExecuteSequence>
This xml configuration has a important attribute and also an important value. The attribute defines at what stage the custom actions are to be executed and the value defines in what procedure.
Here’s a list of possible values for the attribute (in the execution order) This condition can be defined using a before or after attribute:
The procedure when the custom action is to be executed is defined in the value. This post explains the possible values:
This is just a demo, but these scripts could do almost anything to you operating system.
One problem is that if the power shell script execution is not enabled, the installer won’t work.
To check your execution policy, open the power shell and type Get-ExecutionPolicy. The result needs to be RemoteSigned or Unrestricted
Possible values are:
- Restricted – No scripts can be run. Windows PowerShell can be used only in interactive mode.
- AllSigned – Only scripts signed by a trusted publisher can be run.
- RemoteSigned – Downloaded scripts must be signed by a trusted publisher before they can be run.
- Unrestricted – No restrictions; all Windows PowerShell scripts can be run.
Here’s a full working example.