在 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 [password]
A value that specifies the password for the user specified with the /RU parameter. 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.
所以我改成了:
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.
有关完整的工作示例,请参阅
.
回答你的个人questions/issues:
正如您自己猜测的那样,您需要处理 XML 文件路径中的空格。
您需要将路径用双引号引起来。
在 Run
部分,参数列表本身用双引号引起来,您需要将内部双引号加倍:
[Run]
Filename: "schtasks.exe"; \
Parameters: "/create /XML ""{app}\ServerSwitchScheduledTask.xml"" /TN ServerSwitch"
请参阅 Inno Setup 文档中的 Parameters in Sections。
您的 AfterInstall
尝试中的引号正确,但您的可执行文件路径错误。
常量不会在 Code
部分自动解析。
所以要么你只是不指定路径(就像你在 Run
中所做的那样):
if Exec('schtasks.exe', ...)
if Exec(ExpandConstant('{sys}\schtasks.exe'), ...)
您无论如何都应该将其用于参数,以解析安装文件夹:
if Exec(
'schtasks.exe',
ExpandConstant('/create /XML "{app}\ServerSwitchScheduledTask.xml" /tn ServerSwitch'),
...)
至于BeforeInstall
,那简直是废话,因为XML那个时候还没有安装
我正在使用 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 [password]
A value that specifies the password for the user specified with the /RU parameter. 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.
所以我改成了:
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.
有关完整的工作示例,请参阅
回答你的个人questions/issues:
正如您自己猜测的那样,您需要处理 XML 文件路径中的空格。
您需要将路径用双引号引起来。
在 Run
部分,参数列表本身用双引号引起来,您需要将内部双引号加倍:
[Run]
Filename: "schtasks.exe"; \
Parameters: "/create /XML ""{app}\ServerSwitchScheduledTask.xml"" /TN ServerSwitch"
请参阅 Inno Setup 文档中的 Parameters in Sections。
您的 AfterInstall
尝试中的引号正确,但您的可执行文件路径错误。
常量不会在 Code
部分自动解析。
所以要么你只是不指定路径(就像你在 Run
中所做的那样):
if Exec('schtasks.exe', ...)
if Exec(ExpandConstant('{sys}\schtasks.exe'), ...)
您无论如何都应该将其用于参数,以解析安装文件夹:
if Exec(
'schtasks.exe',
ExpandConstant('/create /XML "{app}\ServerSwitchScheduledTask.xml" /tn ServerSwitch'),
...)
至于BeforeInstall
,那简直是废话,因为XML那个时候还没有安装