{"id":632,"date":"2013-10-10T17:51:11","date_gmt":"2013-10-10T21:51:11","guid":{"rendered":"http:\/\/kamivaniea.com\/?p=632"},"modified":"2014-04-28T16:40:07","modified_gmt":"2014-04-28T20:40:07","slug":"using-windows-installer-wix-to-schedule-a-task-as-the-system-user","status":"publish","type":"post","link":"http:\/\/kamivaniea.com\/?p=632","title":{"rendered":"Using Windows Installer (WIX) to schedule a task as the system user"},"content":{"rendered":"<p>Goal: use <a href=\"http:\/\/wixtoolset.org\/\">WIX<\/a> 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.<\/p>\n<p>Answer:<\/p>\n<pre>&lt;Product Id=\"*\" \r\n  Name=\"FooBar\" \r\n  Language=\"1033\" \r\n  Version=\"1.0.0.0\" Manufacturer=\"Foo\" \r\n  UpgradeCode=\"GID\"&gt;\r\n&lt;Package Id=\"*\" \r\n  InstallerVersion=\"200\" \r\n  Compressed=\"yes\" \r\n  InstallScope=\"perMachine\" \r\n  InstallPrivileges=\"elevated\"\/&gt;\r\n     \r\n\r\n&lt;InstallExecuteSequence&gt;\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 &lt;Custom Action=\"CreateScheduledTask\" After=\"InstallFiles\"&gt;\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 NOT Installed\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 &lt;\/Custom&gt;\r\n&lt;\/InstallExecuteSequence&gt;\r\n\r\n&lt;CustomAction Id=\"CreateScheduledTask\" \r\n    Return=\"check\" \r\n    Impersonate=\"no\" \r\n    Execute=\"deferred\"\r\n    Directory=\"TARGETDIR\" \r\n    ExeCommand=\"&amp;quot;[SystemFolder]SCHTASKS.EXE&amp;quot; \/CREATE \/SC MINUTE \/MO 20 \/TN &amp;quot;Foobar&amp;quot;  \/TR &amp;quot;&amp;quot;[INSTALLFOLDER]\\Foobar.exe&amp;quot;&amp;quot; \/RU &amp;quot;NT Authority\\System&amp;quot; \/RP \/RL HIGHEST\" \/&gt;\r\n&lt;\/Product&gt;<\/pre>\n<h3>Explanation:<\/h3>\n<p>To schedule a task you need to create a custom action which calls the command line version of the Windows Task Scheduler (<a href=\"http:\/\/ss64.com\/nt\/schtasks.html\">schtasks.exe<\/a>). In the example above the task is being scheduled to run every 20 minutes starting from when the computer boots (\/SC MINUTE \/MO 20).<\/p>\n<p>The tricky part is making the new scheduled task run as the SYSTEM user with the highest permissions possible (\/RU &#8220;NT Authority\\System&#8221; \/RP \/RL HIGHEST&#8221;). 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=&#8221;elevated&#8221; to the Package element. To run the CustomAction with elevated privileges I added Impersonate=&#8221;no&#8221; and Execute=&#8221;deferred&#8221; to the CustomAction element. <a href=\"http:\/\/blogs.msdn.com\/b\/astebner\/archive\/2006\/12\/13\/some-useful-things-i-have-learned-about-windows-installer-and-uac.aspx\">Aaron Stebner<\/a> explains why deferring the action is necessary. Also note that the use of &#8216;&amp;quot;&#8217; instead of &#8216;&#8221;&#8216; is a feature of WIX and not an error in web formatting.<\/p>\n<p>This solution works, but it is limited by the number of parameters schtasks will take in. This prevents you from doing things like disabling the AC Power requirement. Here is an <a href=\"http:\/\/technet.microsoft.com\/en-us\/library\/cc722156.aspx\">explanation of how to do that<\/a>.<\/p>\n<h3>Getting rid of the black boxes<\/h3>\n<p>Another issue is that a command prompt pops up briefly making the install look messy. To solve the issue you have to use a <a title=\"Quet Execution Custom Action documentation\" href=\"http:\/\/wixtoolset.org\/documentation\/manual\/v3\/customactions\/qtexec.html\">Quiet Execution Custom Action<\/a>.<\/p>\n<pre>&lt;Product Id=\"*\" \r\n  Name=\"FooBar\" \r\n  Language=\"1033\" \r\n  Version=\"1.0.0.0\" Manufacturer=\"Foo\" \r\n  UpgradeCode=\"GID\"&gt;\r\n&lt;Package Id=\"*\" \r\n  InstallerVersion=\"200\" \r\n  Compressed=\"yes\" \r\n  InstallScope=\"perMachine\" \r\n  InstallPrivileges=\"elevated\"\/&gt;\r\n     \r\n\r\n&lt;InstallExecuteSequence&gt;\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 &lt;Custom Action=\"CreateScheduledTask\" After=\"InstallFiles\"&gt;\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 NOT Installed\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 &lt;\/Custom&gt;\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 &lt;Custom Action=\"CreateScheduledTaskId\" After=\"CostFinalize\"&gt;\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 NOT Installed\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 &lt;\/Custom&gt;\r\n&lt;\/InstallExecuteSequence&gt;\r\n\r\n&lt;CustomAction Id=\"CreateScheduledTaskId\" \r\n    Property=\"CreateScheduledTask\"\r\n    Execute=\"immediate\"\r\n    Value=\"&amp;quot;[SystemFolder]SCHTASKS.EXE&amp;quot; \/CREATE \/SC MINUTE \/MO 20 \/TN &amp;quot;Foobar&amp;quot; \/TR &amp;quot;&amp;quot;[INSTALLFOLDER]\\Foobar.exe&amp;quot;&amp;quot; \/RU &amp;quot;NT Authority\\System&amp;quot; \/RP \/RL HIGHEST\" \/&gt;\r\n\r\n&lt;CustomAction Id=\"CreateScheduledTask\" \r\n    Return=\"check\" \r\n    Impersonate=\"no\" \r\n    Execute=\"deferred\"\r\n\u00a0\u00a0\u00a0 BinaryKey=\"WixCA\"\r\n\u00a0\u00a0\u00a0 DllEntry=\"CAQuietExec\" \/&gt;\r\n&lt;\/Product&gt;<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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. Answer: &lt;Product Id=&#8221;*&#8221; Name=&#8221;FooBar&#8221; Language=&#8221;1033&#8243; Version=&#8221;1.0.0.0&#8243; Manufacturer=&#8221;Foo&#8221; UpgradeCode=&#8221;GID&#8221;&gt; &lt;Package Id=&#8221;*&#8221; InstallerVersion=&#8221;200&#8243; Compressed=&#8221;yes&#8221; InstallScope=&#8221;perMachine&#8221; InstallPrivileges=&#8221;elevated&#8221;\/&gt; &lt;InstallExecuteSequence&gt; \u00a0\u00a0\u00a0\u00a0\u00a0 &lt;Custom Action=&#8221;CreateScheduledTask&#8221; After=&#8221;InstallFiles&#8221;&gt; \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 NOT Installed \u00a0\u00a0\u00a0\u00a0\u00a0 &lt;\/Custom&gt; &lt;\/InstallExecuteSequence&gt; [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[7],"tags":[75],"class_list":["post-632","post","type-post","status-publish","format-standard","hentry","category-application-debug","tag-wix"],"jetpack_publicize_connections":[],"jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p2u4LH-ac","jetpack_featured_media_url":"","_links":{"self":[{"href":"http:\/\/kamivaniea.com\/index.php?rest_route=\/wp\/v2\/posts\/632","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/kamivaniea.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/kamivaniea.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/kamivaniea.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/kamivaniea.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=632"}],"version-history":[{"count":10,"href":"http:\/\/kamivaniea.com\/index.php?rest_route=\/wp\/v2\/posts\/632\/revisions"}],"predecessor-version":[{"id":699,"href":"http:\/\/kamivaniea.com\/index.php?rest_route=\/wp\/v2\/posts\/632\/revisions\/699"}],"wp:attachment":[{"href":"http:\/\/kamivaniea.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=632"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/kamivaniea.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=632"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/kamivaniea.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=632"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}