2017-05-09 56 views
1

我有一個WiX安裝程序,其中我有以下自定義操作以在任務計劃程序中創建幾個任務。TaskScheduler不通過命令創建任務

[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; 
     } 

我打電話給他們,如下圖所示在我的WiX文件中。

<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> 

儘管第一個計劃任務被創建,第二個計劃任務卻在任務計劃程序中丟失。日誌確實表明自定義操作已運行。但它在任務調度器中不存在。當我手動運行命令時,它確實被創建。我在這裏做錯了什麼?任何幫助將非常感激。以下是日誌。

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. 
+0

您是否嘗試過更可靠的編程方式? https://msdn.microsoft.com/en-us/library/windows/desktop/aa383448(v=vs.85).aspx – wannadream

+0

我打印了錯誤代碼。這就是我所得到的。 「-2147467259」有什麼想法? :( – mayooran

+0

這是一個運行時錯誤,原因可能會有所不同。很難說。 – wannadream

回答

1

現在,我明白了。從DOC:https://msdn.microsoft.com/en-us/library/windows/desktop/bb736357(v=vs.85).aspx

/SC時間表

指定計劃頻率的值。有效值包括:MINUTE,HOURLY,DAILY,WEEKLY,MONTHLY,ONCE,ONLOGON,ONIDLE和ONEVENT。

+0

哦!我怎麼能通過命令行給這個onstart命令?因爲其他任務調度程序調度方法需要一些代碼更改:( – mayooran

+0

也在這裏http://stackoverflow.com/questions/17438482/set-task-to-run-on-system- startup-schtasks-command-line他們說可以做noh嗎? – mayooran

+0

另外,當我通過命令行手動運行這個命令的時候它的工作! – mayooran

相關問題