2015-12-06 61 views
1

我使用Inno Setup为我的应用程序创建一个安装导入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 [口令]

指定Pa的值ss用于用/ 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. 
+0

我无法重现您的问题。您的Inno安装脚本适用于从Windows Scheduler导出的简单任务。所以我认为这个问题是特定于您的特定任务的,而实际上Inno Setup没有这样的问题。如果您从提升的命令提示符('cmd.exe')执行它,您的'schtasks'命令是否适用于您? –

+0

@MartinPrikryl我认为你是对的,我觉得这与文件路径中的空格有关。我将xml文件安装到'C:\ Program Files(x86)\ Server Too ls \ ServerSwitchScheduledTask.xml'我可以用'C:\ Windows \ system32>命令行从那里运行它schtasks.exe /创建/ XML“C:\ Program Files文件(x86)\服务器太 ls \ ServerSwitchScheduledTask.xml”/ TN ServerSwitch 成功:计划任务“ServerSwitch”已成功创建.'但我不知道如何做到这一点Exec()命令或在“[Run]”部分 – DelightedD0D

+0

是否正在使用您从命令行测试'schtasks.exe'的相同帐户运行安装程序?没有比“无法安排服务器”更多的细节吗? –

回答

1

对于一个完整的工作示例,请参阅
How to add a scheduled task on network connection/disconnection event with Inno Setup


回答您的个别问题/问题:

正如你猜你自己,你需要处理的路径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', ...) 

或使用ExpandConstant function

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

你无论如何都应该使用的参数,解决安装文件夹:

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

至于BeforeInstall,那简直是无稽之谈,因为那时候XML文件还没有安装。

+1

非常感谢。我一整天都在这里,无法完成这项工作。我尝试了你提到的一切,只是没有在正确的组合中。 – DelightedD0D

+0

我注意到如果我想在安装后(使用'postinstall'标志给用户一个选择的结果)执行此操作,我还必须提供'runascurrentuser'标志。我在想这是因为安装后的调用是在不同的环境中,默认情况下不再有安装程序的管理权限。这是否准确? – DelightedD0D

+1

按照[运行部分的文档](http://www.jrsoftware.org/ishelp/index.php?topic=runsection)的说明更正:'未使用** postinstall'时,'runascurrentuser'是默认值* *和'runasoriginaluser'是'postinstall'被**使用**时的默认值。 –