This project has moved. For the latest updates, please go here.

trying to simply change tasks to disabled.

Topics: Examples
Mar 9, 2015 at 1:48 PM
I have attempted the code below,
using (TaskService ts = new TaskService())
{
  Task t = ts.GetTask(taskName);
  if (t != null)
  {
    if (!t.Enabled)
    {
      t.Enabled = true;
      t.RegisterChanges();
    }
  }
}
Unfortunately the code throws an exception and says I need to use the taskfolder.registerTaskDefinition method.  We use a specific account for our tasks.  
I have this method that appears to work, but it does not actually set the task to disabled.
I am missing something. No errors, and the task interface says the task was set to disabled in the history, but the task is never actually set to disabled, and continues to run.
static void enabledisableTasks(bool value)
    {
        using (TaskService TS = new TaskService(td.servername))
        {
            foreach (TasksModified t in listTm)
            {
                try
                {

                    Task task = TS.GetTask(t.path);                        
                    TaskFolder tf = TS.RootFolder;                        
                    task.Enabled = value;
                    tf.RegisterTaskDefinition(task.Path, task.Definition, TaskCreation.CreateOrUpdate, userid, td.password, TaskLogonType.Password,null);                      

                }
                catch (System.Security.SecurityException e)
                {

                    Console.WriteLine(string.Format("Error with accessing {0} do to error {1}", t.tname, e.Message.ToString()));
                }

            }
        }
    }
```
Coordinator
Mar 11, 2015 at 4:48 PM
So, just to be clear, when you run the second piece of code, the task registers successfully and when you look at it via code or through the UI the task is set to NOT enabled? However, the task runs at the triggered times anyway?
Mar 11, 2015 at 5:40 PM
no, the task is still enabled via the UI and Code. but the register is successful in the history of the task. This is what is most strange.
I do have administrative rights to this test environment. AD domain admin.
the task is updated according to the history, but does not register the change.
I have tried connecting to remote server new TaskService(servername, adminuserid,, PW) with and without the id and PW to no avail.
the task shows a status of Ready,
and the trigger is also still at Enabled.
No errors.
  • <Event xmlns="http://schemas.microsoft.com/win/2004/08/events/event">
  • <System>
    <Provider Name="Microsoft-Windows-TaskScheduler" Guid="{DE7B24EA-73C8-4A09-985D-5BDADCFA9017}" />
    <EventID>140</EventID>
    <Version>0</Version>
    <Level>4</Level>
    <Task>140</Task>
    <Opcode>0</Opcode>
    <Keywords>0x8000000000000000</Keywords>
    <TimeCreated SystemTime="2015-03-11T17:16:50.183039000Z" />
    <EventRecordID>323807</EventRecordID>
    <Correlation />
    <Execution ProcessID="1484" ThreadID="10676" />
    <Channel>Microsoft-Windows-TaskScheduler/Operational</Channel>
    <Computer>aserver.ms.ds.uhc.com</Computer>
    <Security UserID="S-1-5-18" />
    </System>
  • <EventData Name="TaskUpdated">
    <Data Name="TaskName">\Testtask1</Data>
    <Data Name="UserName">ms\id</Data>
    </EventData>
    </Event>
  • <Event xmlns="http://schemas.microsoft.com/win/2004/08/events/event">
  • <System>
    <Provider Name="Microsoft-Windows-TaskScheduler" Guid="{DE7B24EA-73C8-4A09-985D-5BDADCFA9017}" />
    <EventID>142</EventID>
    <Version>0</Version>
    <Level>4</Level>
    <Task>142</Task>
    <Opcode>0</Opcode>
    <Keywords>0x8000000000000000</Keywords>
    <TimeCreated SystemTime="2015-03-11T17:16:49.902235400Z" />
    <EventRecordID>323806</EventRecordID>
    <Correlation />
    <Execution ProcessID="1484" ThreadID="12808" />
    <Channel>Microsoft-Windows-TaskScheduler/Operational</Channel>
    <Computer>servername.ms.ds.uhc.com</Computer>
    <Security UserID="S-1-5-18" />
    </System>
  • <EventData Name="TaskDisabled">
    <Data Name="TaskName">\Testtask1</Data>
    <Data Name="UserName">ms\id</Data>
    </EventData>
    </Event>
Mar 17, 2015 at 2:56 PM
Is this the correct process, seems to me that it should work?
Coordinator
Mar 19, 2015 at 8:09 PM
If the task already has been created, the Task.Enabled property does not require re-registration after. You should be able to simply call:
using (TaskService ts = new TaskService())
{
  Task t = ts.GetTask(taskName);
  if (t != null)
  {
    if (!t.Enabled)
    {
      t.Enabled = true;
    }
  }
}
Marked as answer by dahall on 4/20/2015 at 8:14 AM