1

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.
Community
  • 1
  • 1
Wesley Smith
  • 19,401
  • 22
  • 85
  • 133
  • I cannot reproduce your problem. Your Inno Setup scripts works for me for a simple task exported from Windows Scheduler. So I assume the problem is specific to your particular task and has actually nothing to so with Inno Setup as such. Does your `schtasks` command work for you if you execute it from an elevated command prompt (`cmd.exe`)? – Martin Prikryl Dec 06 '15 at 08:32
  • @MartinPrikryl I think you're right, I feel like this has to do with spaces in the path to the file. I'm installing the xml file to `C:\Program Files (x86)\Server Too ls\ServerSwitchScheduledTask.xml` I can run it from there with the command line like `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.` but Im not sure how to do that in the Exec() command or in the `[Run]` section – Wesley Smith Dec 06 '15 at 08:40
  • Are you running the installer using the same account you tested the `schtasks.exe` from command-line? Is there no more details than the "Unable to schedule Server"? – Martin Prikryl Dec 06 '15 at 09:30
  • Can you try to execute command prompt from the `Run` section (like `Filename: "{cmd}"`), execute the installed and once the command prompt pops up, run the `schtasks` manually using the installed `ServerSwitchScheduledTask.xml`? – Martin Prikryl Dec 06 '15 at 09:31
  • @MartinPrikryl yes doing it that way I get: `SUCCESS: The scheduled task "ServerSwitch" has successfully been created.` And when I do it like before, I get a ResultCOde of 267. So it must be an issue with the spaces in the path right? How do I fix that? – Wesley Smith Dec 06 '15 at 09:37
  • What exact command did you use? – Martin Prikryl Dec 06 '15 at 09:38
  • @MartinPrikryl `schtasks.exe /create /XML "C:\Program Files (x86)\Server Too ls\ServerSwitchScheduledTask.xml" /TN ServerSwitch` – Wesley Smith Dec 06 '15 at 09:39
  • I know how to escape it for the command line like that But cant seem to get it right for the RUN or CODE sections – Wesley Smith Dec 06 '15 at 09:39
  • For that the `Run` section equivalent is `Filename: "schtasks.exe"; Parameters: "/create /XML ""{app}\ServerSwitchScheduledTask.xml"" /TN ServerSwitch"` – Martin Prikryl Dec 06 '15 at 09:40

1 Answers1

1

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.

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
  • 1
    Thank you so much. I've been at it all day and could not get this working. I tried everything you mentioned just not in the right combinations. – Wesley Smith Dec 06 '15 at 09:51
  • I noticed that if I want to do this after the install (giving the user a choice at the end) using the `postinstall` flag, I have to also supply the `runascurrentuser` flag. I'm thinking this is because the call made post install is in a different context and no longer has the installers admin rights by default. Is that accurate? – Wesley Smith Dec 06 '15 at 10:39
  • 1
    Correct, as per [the documentation for Run section](http://www.jrsoftware.org/ishelp/index.php?topic=runsection): `runascurrentuser` is the default when `postinstall` is **not used** and the `runasoriginaluser` is the default when `postinstall` is **used**. – Martin Prikryl Dec 06 '15 at 11:45