I'm using Inno Setup to create an installer for my application
How can I add a scheduled task to the users PC from an XML file using Inno Setup?
I have created a scheduled task on my development PC and exported it to a file named 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.
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
Then I read the docs for 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.
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
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 .
To answer your individual questions/issues:
As you guessed yourself, you need to deal with spaces in the path to the XML file.
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]
Filename: "schtasks.exe"; \
Parameters: "/create /XML ""{app}\ServerSwitchScheduledTask.xml"" /TN ServerSwitch"
See Parameters in Sections in Inno Setup documentation.
You have the quotes right in your AfterInstall
attempt, but there you have the executable path wrong.
The constants are not automatically resolved in Code
section.
So either you just do not specify a path (like you do in Run
):
if Exec('schtasks.exe', ...)
or use ExpandConstant
function :
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.