I was recently asked to put together a simple installer for one of our projects. The only installation compiler available was the one that comes with Visual Studio. Whilst limited, this appeared to provide most of the functionality we needed. Any extra work that we needed to perform should have been possible through the "Custom Actions" facility.
I have written this article to help anybody else coming up against this issue. In particular, how to:
- Pass parameters to your script;
- Work with objects such as the File System Object from within your script;
- Interact with the user.
Executing your script
The first step is to get your script executed. Assuming you have already created a Deployment Project within Visual Studio:
- Run your script as a Custom Action - use the Custom Actions Editor to add the script file under the appropriate action folder. For instance, if you want your script to run after install, add it to the "Install" folder.
Now, when you run the compiled installer, your script should be called.
Passing information to your script
Whilst executing your script is a good step forward, you will probably want to send some data to the script when it is executed. If you highlight your script within the Custom Action Editor, the properties window will have four entries:
Name) - the file name of your script;
Condition - a condition that will be tested to determine if your script should be executed or not;
CustomActionData - data to be passed to your script when it is executed;
SourcePath - source location of your script.
CustomActionData can be used to pass any string information you want to the script as it is executed. The data can include information read from the user. For instance, if you have included one of the "Textbox" dialogs in the User Interface Editor, each text box can be given a property name. The text box contents can be passed to the script by putting the property name in
CustomActionData surrounded by square brackets. Other built-in parameters such as
TARGETDIR can be supplied in the same way.
One limitation of
CustomActionData is that it limits you to a single string. If you want to pass multiple parameters, you are on your own. My solution to this was to delimit my parameters with a "|" character. For example:
Within the script, this can be split into separate variables:
var parameters = Session.Property("CustomActionData").split("|");
var targetDir = details;
var url = details;
var smtpAddress = details;
Working with scriptable objects
If you want to interact with scriptable objects such as the File System Object, you just need to create it as you would normally within a standalone script. The code below will create a File System Object, then use it to check if a file exists:
var sharedFso = new ActiveXObject("Scripting.FileSystemObject");
Interacting with the user
Your script can interact with the user by calling back into the installer session. Here is an example of displaying a Yes / No message box:
var msiMessageTypeUser = 0x03000000;
var msiMessageTypeYesNo = 4;
var msiMessageTypeDefault1 = 0x000;
var options = msiMessageTypeUser
var objRecord = Session.Installer.CreateRecord(1);
objRecord.StringData(0) = "";
objRecord.StringData(1) = "Do you want to create the file?";
var response = Session.Message(options, objRecord);
Hopefully, this quick article has given you some useful pointers on how to use scripts from an installer created using a Visual Studio Deployment project. Deployment projects are fairly limited in the functionality they provide. However, the tool is free with Visual Studio, and with the ability to call an arbitrary script, they can be quite powerful.
The download includes a project which illustrates all of the above features.