簡體   English   中英

在 Inno Setup 中從 XML 導入計划任務

[英]Import a scheduled task from XML in Inno Setup

我正在使用Inno Setup為我的應用程序創建安裝程序

如何使用 Inno Setup 從 XML 文件向用戶 PC 添加計划任務?

我在我的開發 PC 上創建了一個計划任務並將其導出到一個名為ServerSwitchScheduledTask.xml的文件中

我已經在我的安裝中包含了這個文件。 我目前將此文件的副本放置在應用程序的文件夾中,如下所示:

[Setup]
PrivilegesRequired=admin

[Files] 
Source: "ServerSwitchScheduledTask.xml"; DestDir: "{app}"; Flags: ignoreversion

這按預期工作。 但是,我還想將計划任務實際導入用戶 PC。

我試過這個

Filename: "schtasks.exe"; \
    Parameters: "/create /XML {app}\ServerSwitchScheduledTask.xml /tn ServerSwitch";

這不會導致我看到的錯誤(在 Inno Setup 的調試輸出中),但不會將計划任務添加到用戶 PC

然后我閱讀了schtasks.exe的文檔

/RP [密碼]

一個值,用於指定使用 /RU 參數指定的用戶的密碼。 要提示輸入密碼,該值必須是“*”或沒有值。 系統帳戶將忽略此密碼。 此參數必須與 /RU 或 /XML 開關結合使用。

所以我把它改成:

Filename: "schtasks.exe"; \
    Parameters: "/create /RP * /XML {app}\ServerSwitchScheduledTask.xml /tn ServerSwitch";

我希望這會在安裝時提示輸入密碼,但它不會也不會生成錯誤或添加計划任務。


我也嘗試過使用這樣的代碼部分:

[Files] 
Source: "ServerSwitchScheduledTask.xml"; DestDir: "{app}"; Flags: ignoreversion; BeforeInstall: BeforeInstallProc

[Code]
procedure BeforeInstallProc;

var
  ResultCode: Integer;     
begin
  { Launch Notepad and wait for it to terminate }
  if Exec('{sys}\schtasks.exe', '/create /XML ServerSwitchScheduledTask.xml /tn ServerSwitch', '', SW_SHOW,
     ewWaitUntilTerminated, ResultCode) then
  begin
    { handle success if necessary; ResultCode contains the exit code }
    MsgBox('Task sceduled', mbConfirmation, MB_OK);
  end
  else begin

  if MsgBox('Unable to schedule Server Switch to start on login. See AUTO RUN  section of the REAM_ME#13#10#13#10Continue anyway?', mbConfirmation, MB_YESNO) = IDYES then
  begin
    { user clicked Yes }
  end;
    { handle failure if necessary; ResultCode contains the error code }
    WizardForm.Close;    
    { MsgBox('Intsataller says: ', mbCriticalError, MB_OK); }
  end;
end;

我收到Unable to schedule Server.....使用此方法顯示的消息


我也試過這樣:

[Files] 
Source: "ServerSwitchScheduledTask.xml"; DestDir: "{app}"; Flags: ignoreversion; AfterInstall: AfterInstallProc

[Code]
procedure AfterInstallProc;

var
  ResultCode: Integer;     
begin
  { Launch Notepad and wait for it to terminate }
  if Exec('{sys}\schtasks.exe', '/create /XML "C:\Program Files (x86)\Server Tools\ServerSwitchScheduledTask.xml" /tn ServerSwitch', '', SW_SHOW,
     ewWaitUntilTerminated, ResultCode) then
  begin
    { handle success if necessary; ResultCode contains the exit code }
    MsgBox('Task sceduled', mbConfirmation, MB_OK);
  end
  else begin

  if MsgBox('Unable to schedule Server Switch to start on login. See AUTO RUN  section of the REAM_ME. Continue anyway?', mbConfirmation, MB_YESNO) = IDYES then
  begin
    { user clicked Yes }
  end;
    { handle failure if necessary; ResultCode contains the error code }
    WizardForm.Close;    
    { MsgBox('Intsataller says: ', mbCriticalError, MB_OK); }
  end;
end;

這也失敗了,但是,安裝文件后,我可以像這樣從命令行成功調用它:

C:\Windows\system32>schtasks.exe /create /XML "C:\Program Files (x86)\Server Too
ls\ServerSwitchScheduledTask.xml" /TN ServerSwitch
SUCCESS: The scheduled task "ServerSwitch" has successfully been created.

有關完整的工作示例,請參閱
如何使用 Inno Setup 在網絡連接/斷開事件上添加計划任務


回答您的個人問題:

正如您自己猜測的那樣,您需要處理 XML 文件路徑中的空格。

您需要將路徑包裝為雙引號。

Run部分,參數列表本身被雙引號包裹,您需要將內部雙引號加倍:

[Run]
Filename: "schtasks.exe"; \
    Parameters: "/create /XML ""{app}\ServerSwitchScheduledTask.xml"" /TN ServerSwitch"

請參閱 Inno Setup 文檔中各節中的參數


您在AfterInstall嘗試中有正確的引號,但是您的可執行路徑錯誤。

Code部分中的常量不會自動解析。

所以要么你只是不指定路徑(就像你在Run所做的那樣):

if Exec('schtasks.exe', ...)

或使用ExpandConstant函數

if Exec(ExpandConstant('{sys}\schtasks.exe'), ...)

無論如何,您應該將其用於參數,以解析安裝文件夾:

if Exec(
     'schtasks.exe',
     ExpandConstant('/create /XML "{app}\ServerSwitchScheduledTask.xml" /tn ServerSwitch'),
     ...)

至於BeforeInstall ,那簡直是無稽之談,因為當時還沒有安裝 XML 文件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM