-
Notifications
You must be signed in to change notification settings - Fork 2
/
taskservice.go
163 lines (157 loc) · 5.13 KB
/
taskservice.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package taskscheduler
import (
"errors"
"time"
"github.com/go-ole/go-ole"
"github.com/go-ole/go-ole/oleutil"
)
// Task is a task found in Windows Task Scheduler 2.0
type Task struct {
Name string
Path string
Enabled bool
LastRunTime time.Time
NextRunTime time.Time
ActionList []ExecAction // Other actions are ignored, we are only interested in Commandline Actions
}
// ExecAction is an action defined in a scheduled Task if type IExecAction.
type ExecAction struct {
WorkingDirectory string
Path string
Arguments string
}
// GetTasks returns a list of all scheduled Tasks in Windows Task Scheduler 2.0
func GetTasks() ([]Task, error) {
// Initialize COM API
if err := ole.CoInitializeEx(0, ole.COINIT_MULTITHREADED); err != nil {
return nil, errors.New("Could not initialize Windows COM API")
}
defer ole.CoUninitialize()
// Create an ITaskService object
unknown, err := ole.CreateInstance(ole.NewGUID("{0F87369F-A4E5-4CFC-BD3E-73E6154572DD}"), nil)
if err != nil {
return nil, errors.New("Could not initialize Task Scheduler 2.0")
}
defer unknown.Release()
// Convert IUnknown to IDispatch to get more functions like CallMethod()
ts, err := unknown.QueryInterface(ole.IID_IDispatch)
if err != nil {
return nil, errors.New("Could not prepare Task Scheduler 2.0")
}
defer ts.Release()
// Connect to the Task Scheduler 2.0
if _, err := ts.CallMethod("Connect", "", "", "", ""); err != nil {
return nil, errors.New("Could not connect to Task Scheduler 2.0")
}
// Get Root Directory of Task Scheduler 2.0 and get all tasks recursively
variant, err := oleutil.CallMethod(ts, "GetFolder", "\\")
if err != nil {
return nil, errors.New("Could not get root folder in Task Scheduler 2.0")
}
root := variant.ToIDispatch()
defer root.Release()
return getTasksRecursively(root), nil
}
func getTasksRecursively(folder *ole.IDispatch) (tasks []Task) {
var (
variant *ole.VARIANT
err error
)
// Get Tasks in subfolders first
if variant, err = oleutil.CallMethod(folder, "GetFolders", int64(0)); err != nil {
return
}
folderIterator := variant.ToIDispatch()
if variant, err = oleutil.GetProperty(folderIterator, "count"); err != nil {
return
}
count, _ := variant.Value().(int32)
for i := int32(1); i <= count; i++ {
// Get Tasks of subfolder i
index := ole.NewVariant(ole.VT_I4, int64(i))
if variant, err = oleutil.GetProperty(folderIterator, "item", &index); err != nil {
continue
}
subfolder := variant.ToIDispatch()
subtasks := getTasksRecursively(subfolder)
tasks = append(tasks, subtasks...)
subfolder.Release()
}
folderIterator.Release()
// Get Tasks
if variant, err = oleutil.CallMethod(folder, "GetTasks", int64(0)); err != nil {
return
}
taskIterator := variant.ToIDispatch()
if variant, err = oleutil.GetProperty(taskIterator, "count"); err != nil {
return
}
count, _ = variant.Value().(int32)
for i := int32(1); i <= count; i++ {
// Get Task i
index := ole.NewVariant(ole.VT_I4, int64(i))
if variant, err = oleutil.GetProperty(taskIterator, "item", &index); err != nil {
continue
}
task := variant.ToIDispatch()
var t Task
if variant, err = oleutil.GetProperty(task, "name"); err == nil {
t.Name = variant.ToString()
}
if variant, err = oleutil.GetProperty(task, "path"); err == nil {
t.Path = variant.ToString()
}
if variant, err = oleutil.GetProperty(task, "enabled"); err == nil {
t.Enabled, _ = variant.Value().(bool)
}
if variant, err = oleutil.GetProperty(task, "lastRunTime"); err == nil {
t.LastRunTime, _ = variant.Value().(time.Time)
}
if variant, err = oleutil.GetProperty(task, "nextRunTime"); err == nil {
t.NextRunTime, _ = variant.Value().(time.Time)
}
// Get more details, e.g. actions
if variant, err = oleutil.GetProperty(task, "definition"); err == nil {
definition := variant.ToIDispatch()
if variant, err = oleutil.GetProperty(definition, "actions"); err == nil {
actions := variant.ToIDispatch()
if variant, err = oleutil.GetProperty(actions, "count"); err == nil {
count2, _ := variant.Value().(int32)
for i := int32(1); i <= count2; i++ {
// Get Action i
index := ole.NewVariant(ole.VT_I4, int64(i))
if variant, err = oleutil.GetProperty(actions, "item", &index); err != nil {
continue
}
action := variant.ToIDispatch()
if variant, err = oleutil.GetProperty(action, "type"); err != nil {
action.Release()
continue
}
actionType, _ := variant.Value().(int32)
if actionType != 0 { // only handle IExecAction
action.Release()
continue
}
var a ExecAction
if variant, err = oleutil.GetProperty(action, "workingDirectory"); err == nil {
a.WorkingDirectory = variant.ToString()
}
if variant, err = oleutil.GetProperty(action, "path"); err == nil {
a.Path = variant.ToString()
}
if variant, err = oleutil.GetProperty(action, "arguments"); err == nil {
a.Arguments = variant.ToString()
}
t.ActionList = append(t.ActionList, a)
action.Release()
}
}
}
}
tasks = append(tasks, t)
task.Release()
}
taskIterator.Release()
return
}