2015-12-06 47 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'被**使用**時的默認值。 –