简体   繁体   English

在 Inno Setup 中从 XML 导入计划任务

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

I'm using Inno Setup to create an installer for my application我正在使用Inno Setup为我的应用程序创建安装程序

How can I add a scheduled task to the users PC from an XML file using Inno Setup?如何使用 Inno Setup 从 XML 文件向用户 PC 添加计划任务?

I have created a scheduled task on my development PC and exported it to a file named ServerSwitchScheduledTask.xml我在我的开发 PC 上创建了一个计划任务并将其导出到一个名为ServerSwitchScheduledTask.xml的文件中

I have included this file in my install.我已经在我的安装中包含了这个文件。 I am currently placing a copy of this file in the application's folder like this:我目前将此文件的副本放置在应用程序的文件夹中,如下所示:

[Setup]
PrivilegesRequired=admin

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

This works as expected.这按预期工作。 However, I would also like to actually import the scheduled task to the users PC.但是,我还想将计划任务实际导入用户 PC。

I tried this我试过这个

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

Which causes no errors that I can see (in the debug output for Inno Setup) but does not add the scheduled task to the users PC这不会导致我看到的错误(在 Inno Setup 的调试输出中),但不会将计划任务添加到用户 PC

Then I read the docs for schtasks.exe然后我阅读了schtasks.exe的文档

/RP [password] /RP [密码]

A value that specifies the password for the user specified with the /RU parameter.一个值,用于指定使用 /RU 参数指定的用户的密码。 To prompt for the password, the value must be either "*" or no value.要提示输入密码,该值必须是“*”或没有值。 This password is ignored for the system account.系统帐户将忽略此密码。 This parameter must be combined with either /RU or the /XML switch.此参数必须与 /RU 或 /XML 开关结合使用。

So I changed it to:所以我把它改成:

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

I expected this to prompt for a password on install but it does not and still does not generate an error or add the scheduled task.我希望这会在安装时提示输入密码,但它不会也不会生成错误或添加计划任务。


I have also tried using the code section like this:我也尝试过使用这样的代码部分:

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

I get the Unable to schedule Server..... message displayed with this method我收到Unable to schedule Server.....使用此方法显示的消息


I also tried like this:我也试过这样:

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

This also failed, however, with the files installed, I can call it from the command line successfully like this:这也失败了,但是,安装文件后,我可以像这样从命令行成功调用它:

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.

For a complete working example, see有关完整的工作示例,请参阅
How to add a scheduled task on network connection/disconnection event with Inno Setup .如何使用 Inno Setup 在网络连接/断开事件上添加计划任务


To answer your individual questions/issues:回答您的个人问题:

As you guessed yourself, you need to deal with spaces in the path to the XML file.正如您自己猜测的那样,您需要处理 XML 文件路径中的空格。

You need to wrap the path to double-quotes.您需要将路径包装为双引号。

In Run section, where the argument list itself is wrapped to double-quotes, you need to double the inner double-quotes:Run部分,参数列表本身被双引号包裹,您需要将内部双引号加倍:

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

See Parameters in Sections in Inno Setup documentation.请参阅 Inno Setup 文档中各节中的参数


You have the quotes right in your AfterInstall attempt, but there you have the executable path wrong.您在AfterInstall尝试中有正确的引号,但是您的可执行路径错误。

The constants are not automatically resolved in Code section. Code部分中的常量不会自动解析。

So either you just do not specify a path (like you do in Run ):所以要么你只是不指定路径(就像你在Run所做的那样):

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

or use ExpandConstant function :或使用ExpandConstant函数

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

You should use that for the parameters anyway, to resolve the installation folder:无论如何,您应该将其用于参数,以解析安装文件夹:

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

As for the BeforeInstall , that's simply a nonsense, as the XML file is not installed yet at that moment.至于BeforeInstall ,那简直是无稽之谈,因为当时还没有安装 XML 文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM