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