Friday, February 8, 2013

Inno Setup: Creating a Windows Scheduled Task

On some occasions, you may need to create a scheduled task in Windows using Inno Setup. Here is an easy way of doing this using the command "schtasks.exe". You should put that in the "Run" section in the script:

Filename: "schtasks.exe"; Parameters: "{code:GetDoModuleTaskAddParams}"; Description: "Module Job Task"; Flags: runhidden; Check: GetDoModuleTask;

function GetModuleTaskAddParams(Default: String):String;
begin
  // This will use the Operation System SYSTEM account for running the task
  result := '/Create /RL HIGHEST /F /TN "Module Job" /SC DAILY /ST 23:59 /RU "" /TR "' + ModuleSubDir + '\' + ModuleFileName;
  result := result + '"';  
end;

7 comments:

Damian Stalls said...

This is helpful, however I am doing the following:

---------------
function GetModuleTaskAddParams(Default: String):String;
begin
// This will use the Operation System SYSTEM account for running the task
result := '/Create /RL HIGHEST /F /TN "Advantage Update" /SC ONLOGON /RU "System" /TR "' + ExpandConstant('{app}\wyUpdate.exe /quickcheck /noerr');
result := result + '"';
end;
---------------

The App folder typically is C:\Program Files (x86\AppName so when the task is created i have a problem where it looks like it sets the following:
PROGRAM: C:\Program
ARGUMENTS: Files (x86\AppName\executable.exe /switch

how can i fix?

Medhavi said...

The space the directory names is causing the problem You should try putting " (double quotes) around {app} and then try again. Let me know if that works since I remember solving this problem earlier and can locate the fix.

Damian Stalls said...

Double quotes seem to cause the task to not be created.

Medhavi said...

Did you try something like this:

ExpandConstant('"{app}\wyUpdate.exe /quickcheck /noerr"'). If not try this as well and post what didn't work for you...

Unknown said...

Use \"" before and after task exe full path name as in following -

Parameters: "/create /tn ""Virtualbox Service Server"" /tr ""\""{app}\VBoxService.exe\"" -tray"" /sc onlogon /rl highest /delay 0000:30";

Source of answer - http://code.google.com/p/virtualboxserverservice/source/browse/branches/release-2.3/vboxservice.iss?r=63

Unknown said...

Use \"" before and after task exe full path name as in following -

Parameters: "/create /tn ""Virtualbox Service Server"" /tr ""\""{app}\VBoxService.exe\"" -tray"" /sc onlogon /rl highest /delay 0000:30";

Source of answer - http://code.google.com/p/virtualboxserverservice/source/browse/branches/release-2.3/vboxservice.iss?r=63

Bkkar said...

hello all
I'm trying to run task schedule to uninstall my software after one year

function GetUninstallDate(Param: string): string;
var
Year, NextYear: string;
begin
// schtasks needs localized date string
Result := GetDateTimeString('ddddd', #0, #0);
// calculate the next year
Year := GetDateTimeString('yyyy', #0, #0);
NextYear := IntToStr(StrToInt(Year) + 1);
StringChange(Result, Year, NextYear);
end;
how could i test it