John McFadyen's profileJohn McFadyens Windows I...PhotosBlogGuestbookMore ![]() | Help |
|
|
April 25 Basic Windows Installer Automation Object (session object)As always I will start a little slow with this and progressively get harder as the posts go on. And if anyone wants to know about anything specific and I have the knowledge to help drops some comments on what you want to know about and I will endeavour to assist. So I guess the first items of interest are the main objects to deal with. Looking at this from a packagers perspective the first part of the automation object you should get your head across is the session object. A session is automatically created when executing an MSI for installation / repair or uninstallation. The session object is very powerful and is one of the key concepts I use in order to create dynamic packages. The simplest yet I believe one of the most important thing to learn as a re/packager is how to set properties using the session object. Although a fairly simple process there are a few caveats around its use. If you haven't already read my posts on Windows Installer sequencing you should read that first as that directly correlates to this post. I usually access the session object using vbscript as a custom action. There are other ways but I find this to be quick and easy. Now taking into account the deferred phase is actually running a script that was generated during the Acquisition / Immediate phase it stands to reason that the session will not be available during the deferred phase. Where the immediate phase is directly communicating with the MSI database we have the access to read / write to that same database. Based on this information we can access the session object during the immediate phases which allows us to access and manipulate table data. (you can even write data back into the msi tables during installation) The most common function we are likely to perform is simply updating / retrieving a property value. So to retrieve a property value its quite simple we just use. strMyValue = session.property("MYPROPERTY") Using the above line the vbscript variable strMyValue will contain the property MYPROPERTY's value. We can also set the properties value by the following line of code. session.property("MYPROPERTY") = "myNewValue" Now its also important to be aware that you must use a vbscript custom action to access the session object. If you use an EXE custom action that calls a vbscript you will not immediately have access to the session. For example if we called cmd /c cscript.exe [SourceDir]MyVbScript.vbs ' Would not allow access to the session object. There is so many reasons how this can be useful. Here is a sample on how I use this functionality to handle applications like AutoCAD which use a different serial number for every machine. In this scenario MACHINEA uses a serial number of ABCD-ABCD-ABCD-ABCD where MACHINEB uses a serial number of DCBA-DCBA-DCBA-DCBA So in order to avoid creating a new transform for every machine that uses AutoCAD I whip up a simple bit of XML (I'm a real fan of XML) and a script that interprets the content of the XML and uses the session object to change the MSI Public Property SERIALNUM to whatever it should be for the appropriate machine. So lets assume our XML looks something like this. <ComputerList> <Computer Name="MACHINEA" SerialNum="ABCD-ABCD-ABCD-ABCD" /> <Computer Name="MACHINEB" SerialNum="DCBA-DCBA-DCBA-DCBA" /> </ComputerList> So now we just whip up a little vbscript to get the name of the current machine during installation. Then we do an XML lookup for that machine and retrieve the appropriate Serial number. Lets assume in this example we are on MACHINEA. Our vbscript looks up that machine name and retrieves the serial ABCD-ABCD-ABCD-ABCD. We then use the following session object syntax to set that property. session.property("SERIALNUM") = "ABCD-ABCD-ABCD-ABCD" Based on this principal we now have a single MSI with a simple custom action that allows us to switch the SERIALNUM property for every machine that installs AutoCAD. So what you may say, well now 3 months down the track we get another 5 machines that want to install AutoCAD. Instead of creating new transforms for each machine we simply update the XML by adding new machines and serial numbers. We don't even have to open an MSI editor to handle this. It can all be done with the simplicity of notepad. So as you see a pretty simple principal with very powerful capabilities. I hope this is useful to some of you! Don't forget requests for anything a little more difficult will keep me blogging. !!! |
|
|