Link to home
Start Free TrialLog in
Avatar of Stef Merlijn
Stef MerlijnFlag for Netherlands

asked on

Delphi create and access TaskScheduler subfolder

Hi,

I'm trying to create subfolder in the TaskScheduler (using Delphi) and create schedules for my application inside the subfolder.
For TaskScheduler-access I use this EE-example.

To create a subfolder I found method TaskFolder.CreateFolder('\MyFolder', ?sddl?);.
Here sddl (=security descriptor associated with the folder) must be set, but I have no idea how to set it correctly.

So as a workaround I tried to create the folder "MyFolder" directly into folder "C:\Windows\System32\tasks\".
The folder is actually created by my application, but the folder is not visible inside Windows TaskScheduler.

procedure TForm1.Button1Click(Sender: TObject);
var ts : TTaskScheduler;
    tf: ITaskFolder;
begin
  CoInitialize(nil);                    // uses ActiveX
  ts := TTaskScheduler.Create(Self);
  try
    ts.AutoConnect := True;
    ts.Connect1(GetNetComputerName);
    if ts.Connected then
    begin

      // ts.GetFolder('\MyFolder', ?sddl? );   << THIS DOESN'T WORK
      // As a workaround I create the folder myself.
      // System32 is changed into SysNative, as otherwise the folder would be created in SysWOW64.
      ForceDirectories('C:\Windows\SysNative\tasks\MyFolder\');

      if DirectoryExists('C:\Windows\SysNative\tasks\MyFolder\') then
        tf := ts.GetFolder('\MyFolder')   // << THIS GIVES AN UNKNOWN ERROR
      else
        tf := ts.GetFolder('\');

Open in new window

How can I create a subfolder for taskscheduler in order to add my application's schedules inside that subfolder?
Avatar of Sinisa Vuk
Sinisa Vuk
Flag of Croatia image

Ah, you are using my old example... For your problem you need to add following differences:
var 
    ts : TTaskScheduler;
    tf: ITaskFolder;
    rootf: ITaskFolder;
   ....
    v: OleVariant;
begin
....

  CoInitialize(nil);
  ts := TTaskScheduler.Create(Self);
  try
    ts.AutoConnect := True;
    ts.Connect1(GetNetComputerName);
    if ts.Connected then
    begin
      rootf := ts.GetFolder('\');

      tf := nil;
      try
        tf := rootf.GetFolder('\MyFolder');
      except
       //if folder not exists - exception will raise...
      end;

      if not Assigned(tf) then
      begin
        TVariantArg(v).VT := VT_BSTR;
        //add rw access as string in SDDL_REVISION_1 format
        TVariantArg(v).bstrVal := StringToOleStr('D:(A;OICI;GRGW;;;AU)');  //discretionary ACL to allow read/write access for authenticated users
        tf := rootf.CreateFolder('\MyFolder', v);
      end;

      ....

Open in new window


Using MS ref on CreateFolder and Security Descriptor String Format def....
Avatar of Stef Merlijn

ASKER

Hi Sinisa Vuk,
Your solution works partly.

It does create a folder, but when I open TaskScheduler itself I cannot delete the task. It sais I'm not authorised to do so (admin-user). And therefore I cannot delete the folder either.
Do you have a solution for that?
I've tried fiddling with the sddl settings, but that is complicated stuff hahaha, how the *** do you know that stuff? Is there some detailed info?

Also: how can I delete the created folder by code?
ASKER CERTIFIED SOLUTION
Avatar of Sinisa Vuk
Sinisa Vuk
Flag of Croatia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
@Sinisa Vuk: Thank you very much.