Raypack: Applying a PowerShell custom action during installation of an MSI
In this blog I will present how we add a custom action to an MSI in order to execute a PowerShell script, with Raypack.
The PowerShell script is simple. It checks if a log-file exists. If true the script appends an entry with the installation time to the file. However the complexity doesn’t matter because using the method described in this blog we can implement any PowerShell script regardless of complexity.
I started by opening a raypack project file (rpp extension). You can create an rpp file either by capturing a setup, or by creating a new empty project. In both situations you are allowed to edit the rpp file and add custom actions. The project in Raypack looks something like this:
We add the files necessary to run the script (we need a batch file and the ps1 script file)
- Files and folders menu
- Create a Folder
- Right click and select Import File(s)…
- Browse and select the file
This is how it looks after we add the file
At the bottom of the screen it is possible to select the Advanced view, followed by selecting the Custom Actions menu
Select the button: Create new…
Choose the Executable Action Type. We are going to run PowerShell.exe with our script as its parameter
PowerShell is on the machine already, so choose the fourth option and click next
Give the Working directory of the file you are executing (AppendToLog.ps1 in Demo dir), followed by the command you want to use. Notice that we are using –ExecutionPolicy Bypass, this is only allowed if you run your custom action in elevated mode, which we will be doing in the next step. It was added to make sure the action will always execute regardless of the current execution policy on the workstation.
The action must be placed as Deferred, this has to do with the PowerShell file needing to be copied on the workstation, and notice the Run as system radio button is selected. This will make sure our script will execute properly(remember we need to run as System because of the -ExecutionPolicy Bypass part of the command line)
Now the Sequencing takes place. Make sure you run during the Normal Installation(as opposed to an Administrative or an Advertised Installation). Sequence dropdown choice should be any action After InstallFiles, but before the action InstallFinalize. This way we make sure that the file is already placed on the machine (InstallFiles) and that the Deferred sequence hasn’t ended (before InstallFinalize).
A final but essential part of the custom action is when it should be executed. In our case we only want to run this script during installation, therefore we use the condition Not Installed. Installed is a property which has a value only during repair or uninstall. So during installation it is empty.
Then its Next Next finish. The result should look like this:
We save and build our project as a Windows Installer.
Then we execute this MSI:
It is completed at 2:05
And we check the log file:
We see a new entry has been added:
In the same way we could run any PowerShell script, without having to worry about the Script ExecutionPolicy on that machine.
Also other types of executables can be executed this way, including cmd.exe which can take parameters such as .bat files and .cmd files, which some administrators would like to run during installation of a product. Remember if you are running cmd.exe use the /c switch which kills the command prompt window after the command completes. For example: Cmd.exe /c “[path]doSomething.bat”
Thank you for taking the time to read this blog.
Authored by: Walter Tavernarakis