Joana Brandão
Joana Brandão

Reputation: 181

Get arguments From Task Scheduler

I can create a new task like this:

 Friend Sub CreateTaskF(ct_NameTask As String, ct_DescriptionTask As String, ct_Hour As Integer, ct_Minut As Integer, ct_date As String, ct_arg As String)
    

    Dim ts As TaskService = New TaskService

    Dim starttime
    starttime = ct_date & "T" & ct_Hour & ":" & ct_Minut & ":00"

    Dim td As TaskDefinition = ts.NewTask
    td.RegistrationInfo.Description = ct_DescriptionTask

    td.Triggers.Add(New DailyTrigger With {.StartBoundary = StartTime})
    td.Actions.Add(New ExecAction(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData).ToString & "\Roaming\Extractor.exe", ct_arg, Nothing))
    ts.RootFolder.RegisterTaskDefinition(ct_NameTask, td)
    
End Sub

But now I need to get the argument of all tasks in Task Scheduler. At this time I receive the count of tasks, and their names, but I don't know how to get the Argument.

Upvotes: 0

Views: 1138

Answers (1)

Joana Brandão
Joana Brandão

Reputation: 181

After a few days of banging my head, I got a solution. I still could not get the arguments, so I created the service, got all the tasks, and looped through a collection of tasks, inside the loop, I get a task file, I read that XML file and from there I have everything about the task I'm working on working.

Friend Function listTask() As DataTable
    Dim dtTasks As New DataTable
    dtTasks.Columns.Add("NAME")
    dtTasks.Columns.Add("STATE")
    dtTasks.Columns.Add("ARGS")

    Dim service
    service = CreateObject("Schedule.Service")
    Call service.Connect()

    ' Get the task folder that contains the tasks. 
    Dim rootFolder
    rootFolder = service.GetFolder("\")

    Dim taskCollection 'As Microsoft.Win32.TaskScheduler.TaskCollection
    taskCollection = rootFolder.GetTasks(0)

    Dim numberOfTasks As Integer
    numberOfTasks = taskCollection.Count


    If numberOfTasks = 0 Then
        MsgBox("Não Existem Tarefas")
    Else
        Dim count As Integer = 0
        Dim registeredTask
        For Each registeredTask In taskCollection
            'For i = 0 To numberOfTasks - 1
            Dim taskState As String
            Select Case registeredTask.State
                Case "0"
                    taskState = "Unknown"
                Case "1"
                    taskState = "Disabled"
                Case "2"
                    taskState = "Queued"
                Case "3"
                    taskState = "Ready"
                Case "4"
                    taskState = "Running"
            End Select
            'TENHO AQUI TUDO !!!!!
            Dim aaaa As String = registeredTask.xml

            Dim BodyDoc As XmlDocument = New XmlDocument()
            BodyDoc.LoadXml(aaaa)
            Dim ReaderDoc As XmlNodeReader = New XmlNodeReader(BodyDoc)

            'TENTAR METER NO DATASET
            Dim ds As DataSet = New DataSet()

            ds.ReadXml(ReaderDoc)
            ReaderDoc.Close()


            dtTasks.Rows.Add()
            dtTasks.Rows(count).Item("NAME") = ds.Tables("RegistrationInfo").Rows(0).Item("URI").ToString
            dtTasks.Rows(count).Item("STATE") = taskState.ToString
            Dim column As DataColumnCollection = ds.Tables("Exec").Columns
            If column.Contains("Arguments") Then
                dtTasks.Rows(count).Item("ARGS") = ds.Tables("Exec").Rows(0).Item("Arguments").ToString
            Else
                dtTasks.Rows(count).Item("ARGS") = ""
            End If
            count = count + 1

        Next
    End If
    Return dtTasks

End Function

Upvotes: 1

Related Questions