• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 94
  • Last Modified:

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?
0
Stef Merlijn
Asked:
Stef Merlijn
  • 3
  • 2
2 Solutions
 
Sinisa VukCommented:
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 VukCommented:
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
 
Sinisa VukCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now