1

I have a WiX installer in which I have the below custom actions to create a couple of tasks in task scheduler.

[CustomAction]
        public static ActionResult CreateScheduleTaskForUpdateTriggering(Session session)
        {
            session.Log("Creating the Scheduled Task");
            string MyAppPath = Environment.GetEnvironmentVariable("MyAppPATH");
            if (!IsTaskExisting("MyAppUpdateTrigger"))
            {
                session.Log("Command Created : " + "C:\\Windows\\System32\\SCHTASKS.exe /Create /TN \"MyAppUpdateTrigger\" /SC ONCE /TR \"" + Path.Combine(new string[] { MyAppPath, "Watchdog", "RunMyAppInstaller.bat" }) + "\" /RL HIGHEST");
                Process p = Process.Start("C:\\Windows\\System32\\SCHTASKS.exe", " /Create /TN \"MyAppUpdateTrigger\" /SC ONCE /TR \"" + Path.Combine(new string[] { MyAppPath, "Watchdog", "RunMyAppInstaller.bat" }) + "\" /ST 00:00:00 /SD 01/01/1990 /RL HIGHEST");
            }
            else
            {
                session.Log("MyAppUpdateTrigger schedule already exists");
            }
            return ActionResult.Success;
        }

        [CustomAction]
        public static ActionResult CreateScheduleTaskForRunningWatchdog(Session session)
        {
            session.Log("Creating the Scheduled Task for running watch dog");

            string MyAppPath = Environment.GetEnvironmentVariable("MyAppPATH");

            if (!IsTaskExisting("RunWatchDog"))
            {
                session.Log("Command Created : " + "C:\\Windows\\System32\\SCHTASKS.exe /Create /TN \"RunWatchDog\" /SC ONSTART /TR \"" + Path.Combine(new string[] { MyAppPath, "Watchdog", "RunWatchDog.bat" }) + "\" /RL HIGHEST");
                Process p = Process.Start("C:\\Windows\\System32\\SCHTASKS.exe", " /Create /TN \"RunWatchDog\" /SC ONSTART /TR \"" + Path.Combine(new string[] { MyAppPath, "Watchdog", "RunWatchDog.bat" }) + "\" /ST 00:00:00 /SD 01/01/1990 /RL HIGHEST");
            }
            return ActionResult.Success;
        }

I am calling these as shown below in my WiX file.

<CustomAction Id="CA_scheduleTaskAction" BinaryKey="removeFolderCustomActionDLL" DllEntry="CreateScheduleTaskForUpdateTriggering" Execute="commit" Return="ignore" />
<CustomAction Id="CA_scheduleTaskActionForWatchDog" BinaryKey="removeFolderCustomActionDLL" DllEntry="CreateScheduleTaskForRunningWatchdog" Execute="commit" Return="ignore" />

<InstallExecuteSequence>
  <!--Custom Action="LaunchWatchdog" After="InstallFinalize" /-->
  <Custom Action="WatchDog.TaskKill" Before="InstallValidate"/>
  <Custom Action="CA_scheduleTaskAction" After="InstallFiles"/>
  <Custom Action="CA_scheduleTaskAction" After="InstallFiles"/>
  <Custom Action="CA_myCustomAction" Before="InstallFinalize">Installed</Custom>
</InstallExecuteSequence>

Though the 1st scheduled task gets created, second one is missing in the task scheduler. Logs do indicate that the custom action has run. But it doesn't exist in the task scheduler. When I run the command manually, it does get created. What am I doing wrong here? Any help would be much appreciated. Below are the logs.

    Calling custom action CustomActionRemoveFolder!CustomActionRemoveFolder.CustomActions.CreateScheduleTaskForRunningWatchdog
Creating the Scheduled Task for running watch dog
Command Created : C:\Windows\System32\SCHTASKS.exe /Create /TN "RunWatchDog" /SC ONSTART /TR "C:\Program Files\Kube2.0\Watchdog\RunWatchDog.bat" /RL HIGHEST
MSI (s) (88:38) [09:42:01:431]: Note: 1: 2318 2:  
MSI (s) (88:38) [09:42:01:431]: No System Restore sequence number for this installation.
MSI (s) (88:38) [09:42:01:431]: Unlocking Server
MSI (s) (88:38) [09:42:01:446]: PROPERTY CHANGE: Deleting UpdateStarted property. Its current value is '1'.
Action ended 9:42:01: InstallFinalize. Return value 1.
Action ended 9:42:01: INSTALL. Return value 1.
AnOldSoul
  • 4,017
  • 12
  • 57
  • 118

1 Answers1

1

Now, I see. From doc: https://msdn.microsoft.com/en-us/library/windows/desktop/bb736357(v=vs.85).aspx

/SC schedule

A value that specifies the schedule frequency. Valid values are: MINUTE, HOURLY, DAILY, WEEKLY, MONTHLY, ONCE, ONLOGON, ONIDLE, and ONEVENT.

wannadream
  • 1,669
  • 1
  • 12
  • 14
  • oh! how can I give this onstart command through command line? Because other methods of task scheduler schedule would need some code change :( – AnOldSoul May 09 '17 at 05:12
  • Also in here http://stackoverflow.com/questions/17438482/set-task-to-run-on-system-startup-schtasks-command-line they say it can be done noh? – AnOldSoul May 09 '17 at 05:13
  • Besides, its working when I manually run this command through commandline! – AnOldSoul May 09 '17 at 05:16
  • Right, the doc is a bit confusing. I am walking thru it now. – wannadream May 09 '17 at 05:18
  • Try this, compare yours to example. https://technet.microsoft.com/en-us/library/cc725744(v=ws.11).aspx#BKMK_startup – wannadream May 09 '17 at 05:22
  • right. so I do recommend you to create it thru programmatic code rather than command line. it's more reliable. – wannadream May 09 '17 at 05:26
  • In that too, there are no examples of running it on startup. People have used Quartz to schedule it. :( – AnOldSoul May 09 '17 at 05:31
  • See here for C# version: http://stackoverflow.com/questions/10833015/task-scheduler-trigger-when-system-boot-in-c-sharp – wannadream May 09 '17 at 05:34