(Whoa, my goal was to hit three posts by the end of the week, but I’ve already hit three today!  Now taking bets on how long until I get bored…)

A fair bit of my days involve some sort of agonizing over WiX.  That’s because I maintain the installers for my employer’s product, which is broken up into three separate logical tiers.  Each tier has its own installer, and none of them are simple.  Two of them involve installing web applications, and the third installs/updates SQL Server databases and installs SQL CLR modules.  As I may have hinted at in previous posts, WiX is terribly painful (but powerful).  It’s like one of my "friends" said as he left me to manage this WiX nightmare: "With great power comes great pain in the @$$". 

Anyway, since I’m having to rework a fair bit of the installers to get them to work on both Windows Server 2003 and 2008 at the same time, I’ll try to throw up any useful snippets that I divine.  First up: installing a task that will run whether the current user is logged in or not:

   1:  <CustomAction Id="CreateScheduledTask" Return="check" Directory="Scripts" 
   2:  ExeCommand="&quot;[SystemFolder]SCHTASKS.EXE&quot; /CREATE /SC DAILY
   3:   /TN &quot;Run Scheduled Queries&quot; /ST 00:00:00 /SD 01/27/2006 /TR 
   4:  .\InvokeQueryScheduler.vbs /RU [%USERDOMAIN]\[LogonUser] /RP" />

So the magic is in invoking "schtasks.exe".  That’s not really anything special, but on Win 2k8, you have to specify the "/RU" flag, or the task won’t run unless the user performing the install is logged on.  If the target server is on a domain, you have to include the domain prefix, which you can get with "[%USERDOMAIN]".  The "[%…]" property allows you to reference an environment variable (in this case, USERDOMAIN, which holds the domain of the current user). 

At this point, I now have one of three installers working correctly on both Windows Server 2003 and Windows Server 2008.  Sadly it is also the most trivial of the installers, so look for more "WiX Snippet" posts as things progress.