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.