October 10, 2013 1 Comment
Goal: use WIX to schedule a task via the task scheduler. The task must run every 30 minutes after the computer starts, it must also run as the SYSTEM user.
<Product Id="*" Name="FooBar" Language="1033" Version="18.104.22.168" Manufacturer="Foo" UpgradeCode="GID"> <Package Id="*" InstallerVersion="200" Compressed="yes" InstallScope="perMachine" InstallPrivileges="elevated"/> ... </Product> <Fragment> <CustomAction Id="CreateScheduledTask" Return="check" Impersonate="no" Execute="deferred" Directory="TARGETDIR" ExeCommand=""[SystemFolder]SCHTASKS.EXE" /CREATE /SC MINUTE /MO 20 /TN "Foobar" /TR "[INSTALLFOLDER]\Foobar.exe" /RU "NT Authority\System" /RP /RL HIGHEST" /> </Fragment>
To schedule a task you need to create a custom action which calls the command line version of the Windows Task Scheduler (schtasks.exe). In the example above the task is being scheduled to run every 20 minutes starting from when the computer boots (/SC MINUTE /MO 20).
The tricky part is making the new scheduled task run as the SYSTEM user with the highest permissions possible (/RU "NT Authority\System" /RP /RL HIGHEST”). To do this the installer itself must run with elevated privileges AND the CustomAction must run with elevated privileges. To run the installer with elevated privileges I added InstallPrivileges=”elevated” to the Package element. To run the CustomAction with elevated privileges I added Impersonate=”no” and Execute=”deferred” to the CustomAction element. Aaron Stebner explains why deferring the action is necessary.
This solution works, but it is limited by the number of parameters schtasks will take in. So you can’t disable the AC Power requirement. Here is an explanation of how to do that.
Another issue is that a command prompt pops up briefly, there is a tutorial on solving that issue.