我正在使用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"
您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文件。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句