-
Notifications
You must be signed in to change notification settings - Fork 29
/
manage.go
682 lines (568 loc) · 21 KB
/
manage.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
// +build windows
package taskmaster
import (
"errors"
"fmt"
"os"
"os/user"
"strings"
"time"
ole "github.com/go-ole/go-ole"
"github.com/go-ole/go-ole/oleutil"
"github.com/rickb777/date/period"
)
// S_FALSE is returned by CoInitialize if it was already called on this thread.
const S_FALSE = 0x00000001
func (t *TaskService) initialize() error {
var err error
err = ole.CoInitialize(0)
if err != nil {
code := err.(*ole.OleError).Code()
if code != ole.S_OK && code != S_FALSE {
return err
}
}
schedClassID, err := ole.ClassIDFrom("Schedule.Service.1")
if err != nil {
ole.CoUninitialize()
return getTaskSchedulerError(err)
}
taskSchedulerObj, err := ole.CreateInstance(schedClassID, nil)
if err != nil {
ole.CoUninitialize()
return getTaskSchedulerError(err)
}
if taskSchedulerObj == nil {
ole.CoUninitialize()
return errors.New("Could not create ITaskService object")
}
defer taskSchedulerObj.Release()
tskSchdlr := taskSchedulerObj.MustQueryInterface(ole.IID_IDispatch)
t.taskServiceObj = tskSchdlr
t.isInitialized = true
return nil
}
// Connect connects to the local Task Scheduler service, using the current
// token for authentication. This function must run before any other functions
// in taskmaster can be used.
func Connect() (TaskService, error) {
return ConnectWithOptions("", "", "", "")
}
// ConnectWithOptions connects to a local or remote Task Scheduler service. This
// function must run before any other functions in taskmaster can be used. If the
// serverName parameter is empty, a connection to the local Task Scheduler service
// will be attempted. If the user and password parameters are empty, the current
// token will be used for authentication.
func ConnectWithOptions(serverName, domain, username, password string) (TaskService, error) {
var err error
var taskService TaskService
if !taskService.isInitialized {
err = taskService.initialize()
if err != nil {
return TaskService{}, fmt.Errorf("error initializing ITaskService object: %v", err)
}
}
_, err = oleutil.CallMethod(taskService.taskServiceObj, "Connect", serverName, username, domain, password)
if err != nil {
return TaskService{}, fmt.Errorf("error connecting to Task Scheduler service: %v", getTaskSchedulerError(err))
}
if serverName == "" {
serverName, err = os.Hostname()
if err != nil {
return TaskService{}, err
}
}
if domain == "" {
domain = serverName
}
if username == "" {
currentUser, err := user.Current()
if err != nil {
return TaskService{}, err
}
username = strings.Split(currentUser.Username, `\`)[1]
}
taskService.connectedDomain = domain
taskService.connectedComputerName = serverName
taskService.connectedUser = username
res, err := oleutil.CallMethod(taskService.taskServiceObj, "GetFolder", `\`)
if err != nil {
return TaskService{}, fmt.Errorf("error getting the root folder: %v", getTaskSchedulerError(err))
}
taskService.rootFolderObj = res.ToIDispatch()
taskService.isConnected = true
return taskService, nil
}
// Disconnect frees all the Task Scheduler COM objects that have been created.
// If this function is not called before the parent program terminates,
// memory leaks will occur.
func (t *TaskService) Disconnect() {
if t.isConnected {
t.taskServiceObj.Release()
t.rootFolderObj.Release()
}
if t.isInitialized {
ole.CoUninitialize()
}
t.isInitialized = false
t.isConnected = false
}
// GetRunningTasks enumerates the Task Scheduler database for all currently running tasks.
func (t *TaskService) GetRunningTasks() (RunningTaskCollection, error) {
var runningTasks RunningTaskCollection
res, err := oleutil.CallMethod(t.taskServiceObj, "GetRunningTasks", TASK_ENUM_HIDDEN)
if err != nil {
return nil, fmt.Errorf("error getting running tasks: %v", getTaskSchedulerError(err))
}
runningTasksObj := res.ToIDispatch()
defer runningTasksObj.Release()
err = oleutil.ForEach(runningTasksObj, func(v *ole.VARIANT) error {
task := v.ToIDispatch()
runningTask, err := parseRunningTask(task)
if err != nil {
return fmt.Errorf("error parsing running task: %v", err)
}
runningTasks = append(runningTasks, runningTask)
return nil
})
if err != nil {
return nil, err
}
return runningTasks, nil
}
// GetRegisteredTasks enumerates the Task Scheduler database for all currently registered tasks.
func (t *TaskService) GetRegisteredTasks() (RegisteredTaskCollection, error) {
var (
err error
registeredTasks RegisteredTaskCollection
)
// get tasks from root folder
res, err := oleutil.CallMethod(t.rootFolderObj, "GetTasks", int(TASK_ENUM_HIDDEN))
if err != nil {
return nil, fmt.Errorf("error getting tasks of root folder: %v", getTaskSchedulerError(err))
}
rootTaskCollection := res.ToIDispatch()
defer rootTaskCollection.Release()
err = oleutil.ForEach(rootTaskCollection, func(v *ole.VARIANT) error {
task := v.ToIDispatch()
registeredTask, path, err := parseRegisteredTask(task)
if err != nil {
return fmt.Errorf("error parsing registered task %s: %v", path, err)
}
registeredTasks = append(registeredTasks, registeredTask)
return nil
})
if err != nil {
return nil, err
}
res, err = oleutil.CallMethod(t.rootFolderObj, "GetFolders", 0)
if err != nil {
return nil, fmt.Errorf("error getting task folders of root folder: %v", getTaskSchedulerError(err))
}
taskFolderList := res.ToIDispatch()
defer taskFolderList.Release()
// recursively enumerate folders and tasks
var enumTaskFolders func(*ole.VARIANT) error
enumTaskFolders = func(v *ole.VARIANT) error {
taskFolder := v.ToIDispatch()
defer taskFolder.Release()
res, err := oleutil.CallMethod(taskFolder, "GetTasks", int(TASK_ENUM_HIDDEN))
if err != nil {
return fmt.Errorf("error getting tasks of folder: %v", getTaskSchedulerError(err))
}
taskCollection := res.ToIDispatch()
defer taskCollection.Release()
err = oleutil.ForEach(taskCollection, func(v *ole.VARIANT) error {
task := v.ToIDispatch()
registeredTask, path, err := parseRegisteredTask(task)
if err != nil {
return fmt.Errorf("error parsing registered task %s: %v", path, err)
}
registeredTasks = append(registeredTasks, registeredTask)
return nil
})
if err != nil {
return err
}
res, err = oleutil.CallMethod(taskFolder, "GetFolders", 0)
if err != nil {
return fmt.Errorf("error getting subfolders of folder: %v", getTaskSchedulerError(err))
}
taskFolderList := res.ToIDispatch()
defer taskFolderList.Release()
err = oleutil.ForEach(taskFolderList, enumTaskFolders)
if err != nil {
return err
}
return nil
}
err = oleutil.ForEach(taskFolderList, enumTaskFolders)
if err != nil {
return nil, err
}
return registeredTasks, nil
}
// GetRegisteredTask attempts to find the specified registered task and returns a
// pointer to it if it exists. If it doesn't exist, nil will be returned in place of
// the registered task.
func (t *TaskService) GetRegisteredTask(path string) (RegisteredTask, error) {
if path[0] != '\\' {
return RegisteredTask{}, ErrInvalidPath
}
taskObj, err := oleutil.CallMethod(t.rootFolderObj, "GetTask", path)
if err != nil {
return RegisteredTask{}, fmt.Errorf("error getting registered task %s: %v", path, getTaskSchedulerError(err))
}
task, _, err := parseRegisteredTask(taskObj.ToIDispatch())
if err != nil {
return RegisteredTask{}, fmt.Errorf("error parsing registered task %s: %v", path, err)
}
return task, nil
}
// GetTaskFolders enumerates the Task Schedule database for all task folders and currently
// registered tasks.
func (t TaskService) GetTaskFolders() (TaskFolder, error) {
return t.GetTaskFolder(`\`)
}
// GetTaskFolder enumerates the Task Schedule database for all task sub folders and currently
// registered tasks under the folder specified, if it exists. If it doesn't exist, nil will be
// returned in place of the task folder.
func (t TaskService) GetTaskFolder(path string) (TaskFolder, error) {
if path[0] != '\\' {
return TaskFolder{}, ErrInvalidPath
}
var topFolderObj *ole.IDispatch
if path == `\` {
topFolderObj = t.rootFolderObj
} else {
topFolder, err := oleutil.CallMethod(t.taskServiceObj, "GetFolder", path)
if err != nil {
return TaskFolder{}, fmt.Errorf("error getting folder %s: %v", path, getTaskSchedulerError(err))
}
topFolderObj = topFolder.ToIDispatch()
defer topFolderObj.Release()
}
// get tasks from the top folder
res, err := oleutil.CallMethod(topFolderObj, "GetTasks", int(TASK_ENUM_HIDDEN))
if err != nil {
return TaskFolder{}, fmt.Errorf("error getting tasks of folder %s: %v", path, getTaskSchedulerError(err))
}
topFolderTaskCollection := res.ToIDispatch()
defer topFolderTaskCollection.Release()
topFolder := TaskFolder{Path: `\`}
err = oleutil.ForEach(topFolderTaskCollection, func(v *ole.VARIANT) error {
task := v.ToIDispatch()
registeredTask, path, err := parseRegisteredTask(task)
if err != nil {
return fmt.Errorf("error parsing registered task %s: %v", path, err)
}
topFolder.RegisteredTasks = append(topFolder.RegisteredTasks, registeredTask)
return nil
})
if err != nil {
return TaskFolder{}, err
}
res, err = oleutil.CallMethod(topFolderObj, "GetFolders", 0)
if err != nil {
return TaskFolder{}, fmt.Errorf("error getting subfolders of folder %s: %v", path, getTaskSchedulerError(err))
}
taskFolderList := res.ToIDispatch()
defer taskFolderList.Release()
// recursively enumerate folders and tasks
var initEnumTaskFolders func(*TaskFolder) func(*ole.VARIANT) error
initEnumTaskFolders = func(parentFolder *TaskFolder) func(*ole.VARIANT) error {
var enumTaskFolders func(*ole.VARIANT) error
enumTaskFolders = func(v *ole.VARIANT) error {
taskFolder := v.ToIDispatch()
defer taskFolder.Release()
name := oleutil.MustGetProperty(taskFolder, "Name").ToString()
path := oleutil.MustGetProperty(taskFolder, "Path").ToString()
res, err := oleutil.CallMethod(taskFolder, "GetTasks", int(TASK_ENUM_HIDDEN))
if err != nil {
return fmt.Errorf("error getting tasks of folder %s: %v", path, getTaskSchedulerError(err))
}
taskCollection := res.ToIDispatch()
defer taskCollection.Release()
taskSubFolder := &TaskFolder{
Name: name,
Path: path,
}
err = oleutil.ForEach(taskCollection, func(v *ole.VARIANT) error {
task := v.ToIDispatch()
registeredTask, path, err := parseRegisteredTask(task)
if err != nil {
return fmt.Errorf("error parsing registered task %s: %v", path, err)
}
taskSubFolder.RegisteredTasks = append(taskSubFolder.RegisteredTasks, registeredTask)
return nil
})
if err != nil {
return err
}
parentFolder.SubFolders = append(parentFolder.SubFolders, taskSubFolder)
res, err = oleutil.CallMethod(taskFolder, "GetFolders", 0)
if err != nil {
return fmt.Errorf("error getting subfolders of folder %s: %v", path, getTaskSchedulerError(err))
}
taskFolderList := res.ToIDispatch()
defer taskFolderList.Release()
err = oleutil.ForEach(taskFolderList, initEnumTaskFolders(taskSubFolder))
if err != nil {
return err
}
return nil
}
return enumTaskFolders
}
err = oleutil.ForEach(taskFolderList, initEnumTaskFolders(&topFolder))
if err != nil {
return TaskFolder{}, err
}
return topFolder, nil
}
// NewTaskDefinition returns a new task definition that can be used to register a new task.
// Task settings and properties are set to Task Scheduler default values.
func (t TaskService) NewTaskDefinition() Definition {
var newDef Definition
newDef.Principal.LogonType = TASK_LOGON_INTERACTIVE_TOKEN
newDef.Principal.RunLevel = TASK_RUNLEVEL_LUA
newDef.RegistrationInfo.Author = t.connectedDomain + `\` + t.connectedUser
newDef.RegistrationInfo.Date = time.Now()
newDef.Settings.AllowDemandStart = true
newDef.Settings.AllowHardTerminate = true
newDef.Settings.Compatibility = TASK_COMPATIBILITY_V2
newDef.Settings.DontStartOnBatteries = true
newDef.Settings.Enabled = true
newDef.Settings.Hidden = false
newDef.Settings.IdleSettings.IdleDuration = period.NewHMS(0, 10, 0) // PT10M
newDef.Settings.IdleSettings.WaitTimeout = period.NewHMS(1, 0, 0) // PT1H
newDef.Settings.MultipleInstances = TASK_INSTANCES_IGNORE_NEW
newDef.Settings.Priority = 7
newDef.Settings.RestartCount = 0
newDef.Settings.RestartOnIdle = false
newDef.Settings.RunOnlyIfIdle = false
newDef.Settings.RunOnlyIfNetworkAvailable = false
newDef.Settings.StartWhenAvailable = false
newDef.Settings.StopIfGoingOnBatteries = true
newDef.Settings.StopOnIdleEnd = true
newDef.Settings.TimeLimit = period.NewHMS(72, 0, 0) // PT72H
newDef.Settings.WakeToRun = false
return newDef
}
// CreateTask creates a registered task on the connected computer. CreateTask returns
// true if the task was successfully registered, and false if the overwrite parameter
// is false and a task at the specified path already exists.
func (t *TaskService) CreateTask(path string, newTaskDef Definition, overwrite bool) (RegisteredTask, bool, error) {
return t.CreateTaskEx(path, newTaskDef, "", "", newTaskDef.Principal.LogonType, overwrite)
}
// CreateTaskEx creates a registered task on the connected computer. CreateTaskEx returns
// true if the task was successfully registered, and false if the overwrite parameter
// is false and a task at the specified path already exists.
func (t *TaskService) CreateTaskEx(path string, newTaskDef Definition, username, password string, logonType TaskLogonType, overwrite bool) (RegisteredTask, bool, error) {
var err error
if path[0] != '\\' {
return RegisteredTask{}, false, ErrInvalidPath
} else if err = validateDefinition(newTaskDef); err != nil {
return RegisteredTask{}, false, err
}
nameIndex := strings.LastIndex(path, `\`)
folderPath := path[:nameIndex]
if !t.taskFolderExist(folderPath) {
_, err = oleutil.CallMethod(t.rootFolderObj, "CreateFolder", folderPath, "")
if err != nil {
return RegisteredTask{}, false, fmt.Errorf("error creating folder %s: %v", path, getTaskSchedulerError(err))
}
} else {
if t.registeredTaskExist(path) {
if !overwrite {
task, err := t.GetRegisteredTask(path)
if err != nil {
return RegisteredTask{}, false, err
}
return task, false, nil
}
_, err = oleutil.CallMethod(t.rootFolderObj, "DeleteTask", path, 0)
if err != nil {
return RegisteredTask{}, false, fmt.Errorf("error deleting registered task %s: %v", path, getTaskSchedulerError(err))
}
}
}
newTaskObj, err := t.modifyTask(path, newTaskDef, username, password, logonType, TASK_CREATE)
if err != nil {
return RegisteredTask{}, false, fmt.Errorf("error creating registered task %s: %v", path, err)
}
newTask, _, err := parseRegisteredTask(newTaskObj)
if err != nil {
return RegisteredTask{}, false, fmt.Errorf("error parsing registered task %s: %v", path, err)
}
return newTask, true, nil
}
// UpdateTask updates a registered task.
func (t *TaskService) UpdateTask(path string, newTaskDef Definition) (RegisteredTask, error) {
return t.UpdateTaskEx(path, newTaskDef, "", "", newTaskDef.Principal.LogonType)
}
// UpdateTaskEx updates a registered task.
func (t *TaskService) UpdateTaskEx(path string, newTaskDef Definition, username, password string, logonType TaskLogonType) (RegisteredTask, error) {
var err error
if path[0] != '\\' {
return RegisteredTask{}, ErrInvalidPath
} else if err = validateDefinition(newTaskDef); err != nil {
return RegisteredTask{}, err
}
newTaskObj, err := t.modifyTask(path, newTaskDef, username, password, logonType, TASK_UPDATE)
if err != nil {
return RegisteredTask{}, fmt.Errorf("error updating %s task: %v", path, err)
}
// update the internal database of registered tasks
newTask, _, err := parseRegisteredTask(newTaskObj)
if err != nil {
return RegisteredTask{}, fmt.Errorf("error parsing registered task %s: %v", path, err)
}
return newTask, nil
}
func (t *TaskService) modifyTask(path string, newTaskDef Definition, username, password string, logonType TaskLogonType, flags TaskCreationFlags) (*ole.IDispatch, error) {
// set default UserID if UserID and GroupID both aren't set
if newTaskDef.Principal.UserID == "" && newTaskDef.Principal.GroupID == "" {
newTaskDef.Principal.UserID = t.connectedDomain + `\` + t.connectedUser
}
res, err := oleutil.CallMethod(t.taskServiceObj, "NewTask", 0)
if err != nil {
return nil, fmt.Errorf("error creating new task: %v", getTaskSchedulerError(err))
}
newTaskDefObj := res.ToIDispatch()
defer newTaskDefObj.Release()
err = fillDefinitionObj(newTaskDef, newTaskDefObj)
if err != nil {
return nil, fmt.Errorf("error filling ITaskDefinition: %v", err)
}
newTaskObj, err := oleutil.CallMethod(t.rootFolderObj, "RegisterTaskDefinition", path, newTaskDefObj, int(flags), username, password, int(logonType), "")
if err != nil {
return nil, fmt.Errorf("error registering task: %v", getTaskSchedulerError(err))
}
return newTaskObj.ToIDispatch(), nil
}
// DeleteFolder removes a task folder from the connected computer. If the deleteRecursively parameter
// is set to true, all tasks and subfolders will be removed recursively. If it's set to false, DeleteFolder
// will return true if the folder was empty and deleted successfully, and false otherwise.
func (t *TaskService) DeleteFolder(path string, deleteRecursively bool) (bool, error) {
var err error
if path[0] != '\\' {
return false, ErrInvalidPath
}
taskFolder, err := oleutil.CallMethod(t.taskServiceObj, "GetFolder", path)
if err != nil {
return false, fmt.Errorf("error getting folder: %v", getTaskSchedulerError(err))
}
taskFolderObj := taskFolder.ToIDispatch()
defer taskFolderObj.Release()
res, err := oleutil.CallMethod(taskFolderObj, "GetTasks", int(TASK_ENUM_HIDDEN))
if err != nil {
return false, fmt.Errorf("error getting tasks of folder: %v", getTaskSchedulerError(err))
}
taskCollection := res.ToIDispatch()
defer taskCollection.Release()
if !deleteRecursively && oleutil.MustGetProperty(taskCollection, "Count").Val > 0 {
return false, nil
}
res, err = oleutil.CallMethod(taskFolderObj, "GetFolders", int(TASK_ENUM_HIDDEN))
if err != nil {
return false, fmt.Errorf("error getting the subfolders: %v", getTaskSchedulerError(err))
}
folderCollection := res.ToIDispatch()
defer folderCollection.Release()
if !deleteRecursively && oleutil.MustGetProperty(folderCollection, "Count").Val > 0 {
return false, nil
}
if deleteRecursively {
// delete tasks in parent folder
deleteAllTasks := func(v *ole.VARIANT) error {
taskObj := v.ToIDispatch()
defer taskObj.Release()
name := oleutil.MustGetProperty(taskObj, "Path").ToString()
return t.DeleteTask(name)
}
err = oleutil.ForEach(taskCollection, deleteAllTasks)
if err != nil {
return false, err
}
var deleteTasksRecursively func(*ole.VARIANT) error
deleteTasksRecursively = func(v *ole.VARIANT) error {
var err error
folderObj := v.ToIDispatch()
defer folderObj.Release()
res, err := oleutil.CallMethod(folderObj, "GetTasks", int(TASK_ENUM_HIDDEN))
if err != nil {
return fmt.Errorf("error getting tasks of folder: %v", getTaskSchedulerError(err))
}
tasks := res.ToIDispatch()
defer tasks.Release()
err = oleutil.ForEach(tasks, deleteAllTasks)
if err != nil {
return err
}
res, err = oleutil.CallMethod(folderObj, "GetFolders", int(TASK_ENUM_HIDDEN))
if err != nil {
return fmt.Errorf("error getting subfolders: %v", getTaskSchedulerError(err))
}
subFolders := res.ToIDispatch()
defer subFolders.Release()
err = oleutil.ForEach(subFolders, deleteTasksRecursively)
if err != nil {
return err
}
currentFolderPath := oleutil.MustGetProperty(folderObj, "Path").ToString()
_, err = oleutil.CallMethod(t.rootFolderObj, "DeleteFolder", currentFolderPath, 0)
if err != nil {
return fmt.Errorf("error deleting task folder %s: %v", path, getTaskSchedulerError(err))
}
return nil
}
// delete all subfolders and tasks recursively
err = oleutil.ForEach(folderCollection, deleteTasksRecursively)
if err != nil {
return false, err
}
}
// delete parent folder
_, err = oleutil.CallMethod(t.rootFolderObj, "DeleteFolder", path, 0)
if err != nil {
return false, fmt.Errorf("error deleting task folder %s: %v", path, getTaskSchedulerError(err))
}
return true, nil
}
// DeleteTask removes a registered task from the connected computer.
func (t *TaskService) DeleteTask(path string) error {
var err error
if path[0] != '\\' {
return ErrInvalidPath
}
_, err = oleutil.CallMethod(t.rootFolderObj, "DeleteTask", path, 0)
if err != nil {
return fmt.Errorf("error deleting task %s: %v", path, getTaskSchedulerError(err))
}
return nil
}
func (t *TaskService) registeredTaskExist(path string) bool {
_, err := oleutil.CallMethod(t.rootFolderObj, "GetTask", path)
if err != nil {
if getOLEErrorCode(err) == 0x80070002 {
return false
}
// trying to get the task resulted in an error, but the task technically exists,
// so we'll return true
return true
}
return true
}
func (t *TaskService) taskFolderExist(path string) bool {
_, err := oleutil.CallMethod(t.taskServiceObj, "GetFolder", path)
if err != nil {
if getOLEErrorCode(err) == 0x80070002 {
return false
}
// trying to get the task folder resulted in an error, but the task foler
// technically exists, so we'll return true
return true
}
return true
}