Avatar of Stef Merlijn
Stef Merlijn
Flag 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?
Delphi

Avatar of undefined
Last Comment
Stef Merlijn

8/22/2022 - Mon
Sinisa Vuk

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....
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
Sinisa Vuk

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Stef Merlijn

ASKER
@Sinisa Vuk: Thank you very much.
Your help has saved me hundreds of hours of internet surfing.
fblack61