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?
Stef MerlijnDeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Sinisa VukSoftware architectCommented:
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....
0
Stef MerlijnDeveloperAuthor Commented:
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?
0
Sinisa VukSoftware architectCommented:
you can delete this way:
...
  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;
     
     //delete old instance
     try
        tf.DeleteTask('MyBackupTask', 0);  //delete if exists
     except
     end;
...

Open in new window


...and when you delete task from code.... empty folder can be deleted from Task folders...
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Sinisa VukSoftware architectCommented:
Put this sddl value: StringToOleStr('D:(A;OICI;FA;;;BU)')
It is read as set full access (FA) to all built-in users (BU). if you want to be able for admin only - put BA (built-in admin) instead of BU.
0
Stef MerlijnDeveloperAuthor Commented:
@Sinisa Vuk: Thank you very much.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.