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

Trying to disable a task (vb)

Topics: Errors
Aug 12, 2015 at 3:12 PM
Hi, I create a task using the following code...


Try
        Dim ts As New TaskService()

        ' Create a new task definition and assign properties
        Dim td As TaskDefinition = ts.NewTask()
        td.RegistrationInfo.Description = "Does something"
        td.Principal.RunLevel = TaskRunLevel.Highest
        td.Principal.LogonType = TaskLogonType.InteractiveToken

        ' Create a trigger that will fire the task at 06:00 every day.
        Dim dt As New DailyTrigger(1)
        dt.StartBoundary = CDate(DateAdd(DateInterval.Day, 1, Now()).ToString("dd/MM/yyyy 06:00:00"))
        td.Triggers.Add(dt)

        'create a trigger that will fire the task every thursday at 04:00.
        Dim wt As WeeklyTrigger = DirectCast(td.Triggers.Add(New WeeklyTrigger()), WeeklyTrigger)
        wt.StartBoundary = CDate(DateAdd(DateInterval.Day, 1, Now()).ToString("dd/MM/yyyy 04:00:00"))
        wt.DaysOfWeek = DaysOfTheWeek.Thursday

        ' Create an action that will launch Notepad whenever the trigger fires
        td.Actions.Add(New ExecAction("notepad.exe", "c:\test.txt", Nothing))

        ' Register the task in the root folder
        ts.RootFolder.RegisterTaskDefinition("TestTask", td)

    Catch ex As Exception
        MsgBox("error - " & ex.Message)
    End Try

...and I'm trying to disable it using the following
    Try
        Dim ts As New TaskService

        Dim t As Task = ts.GetTask("TestTask")
        t.Enabled = False
        t.RegisterChanges()
    Catch ex As Exception
        MsgBox("error - " & ex.Message)
    End Try

...but when I use the code to disable it, the code runs OK, but the task isn't disabled.

What am I missing?

PS - the code to define the triggers looks a bit messy as I'm still getting my head around this - sorry.
Coordinator
Aug 12, 2015 at 9:17 PM
See this answer for detail on the Enabled property: https://taskscheduler.codeplex.com/discussions/585279

For your code, there are some shortcuts in VB that will make your code easier to read:
Dim ts As New TaskService()

' Create a new task definition and assign properties
Dim td As TaskDefinition = ts.NewTask()
td.RegistrationInfo.Description = "Does something"
td.Principal.RunLevel = TaskRunLevel.Highest
td.Principal.LogonType = TaskLogonType.InteractiveToken

' Create a trigger that will fire the task at 06:00 every day.
td.Triggers.Add(New DailyTrigger(1) With {.StartBoundary = DateTime.Today.AddHours(6) })

' Create a trigger that will fire the task every Thursday at 04:00.
td.Triggers.Add(New WeeklyTrigger With {.StartBoundary = DateTime.Today.AddHours(4), .DaysOfWeek = DaysOfTheWeek.Thursday })

' Create an action that will launch Notepad whenever the trigger fires
td.Actions.Add("notepad.exe", "c:\test.txt")

' Register the task in the root folder
ts.RootFolder.RegisterTaskDefinition("TestTask", td)
Aug 13, 2015 at 4:16 PM
Thanks for the tidier code. And I'm sure I tried that method to disable the task, but it's working now, so thank you!
Aug 14, 2015 at 11:33 AM
Update - It work on tasks that I've created.

When I try to disable a task created by someone else, and the machine name has subsequently changed, I can't disable the task.

The code throws an exception - "No mapping between account names and security IDs was done (Exception from HRESULT 0x80070534)"

Also, if I try to disable the task by right-clicking it in task scheduler, it won't let me, displaying an error message saying 'The specified account name is not valid'. In order to fix this, I have to go into the properties of the task and change the user account.

Strangely, the task with the incorrect account name still runs, so how would I change the account name so that I can disable it?
Coordinator
Aug 14, 2015 at 6:15 PM
I believe you just need to create an instance of the existing task and the re-register the task using new credentials. The samples page shows how to get a current task instance. You will need to supply the new user information in the RegisterTaskDefinition method.
Aug 19, 2015 at 10:39 AM
Edited Aug 27, 2015 at 4:38 PM
OK,

That's what I thought, but it's not working for me, so here's the code I'm trying, could you tell me what I'm doing wrong please?
    'ask for the task name
    Dim Taskname As String = InputBox("What's the task name?")
    Dim td As TaskDefinition = Nothing

    Try
        'see if the username is set to something.
        Dim ts As New TaskService
        Dim thetask As Task = ts.GetTask(Taskname)
        Dim Authorname As String = "NotGood"
        Try
            td = thetask.Definition
            Authorname = td.RegistrationInfo.Author.ToString
        Catch ex As Exception
            Authorname = "NotGood"
        End Try

        'if we get a name, then this task was created by the 'wrong' user - try creating the task again to change the author.
        If Authorname <> "NotGood" Then
            td.Principal.RunLevel = TaskRunLevel.Highest
            td.Principal.LogonType = TaskLogonType.InteractiveToken
            td.RegistrationInfo.Author = "tevalis"
            thetask.RegisterChanges()
            ts.RootFolder.RegisterTaskDefinition(Taskname, td)
        End If

    Catch ex As Exception
        MsgBox("Error - " & ex.Message)
    End Try
...should the RootFolder.RegisterTaskDefinition line use the xml? - if so, how do I get the xml of the existing task?