Skip to content

Commit f331f6a

Browse files
committed
check_tasksched: misc improvements
add hidden argument to check, this is used while discovering tasks in the embedded powershell script directly. add debug to stderr calls for the folder,title, recursive and hidden parameters/args in the script add logic to remove trailing backslash in the folder path, is only removed if its not root path . this is needed due to new script using scheduler.service com tasks change the working_dir attribute to working_directory to match with existing check definitions
1 parent 97d7087 commit f331f6a

3 files changed

Lines changed: 40 additions & 3 deletions

File tree

pkg/snclient/check_tasksched.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,22 @@ type CheckTasksched struct {
1414
TaskTitle string
1515
Folder string
1616
Recursive bool
17+
Hidden bool
1718
}
1819

1920
const (
2021
CheckTaskschedDefaultTaskTitle string = "*"
2122
CheckTaskschedDefaultFolder string = "\\"
2223
CheckTaskschedDefaultRecursive bool = true
24+
CheckTaskschedDefaultHidden bool = false
2325
)
2426

2527
func NewCheckTasksched() CheckHandler {
2628
return &CheckTasksched{
2729
TaskTitle: CheckTaskschedDefaultTaskTitle,
2830
Folder: CheckTaskschedDefaultFolder,
2931
Recursive: CheckTaskschedDefaultRecursive,
32+
Hidden: CheckTaskschedDefaultHidden,
3033
}
3134
}
3235

@@ -43,6 +46,7 @@ func (l *CheckTasksched) Build() *CheckData {
4346
"title": {value: &l.TaskTitle, description: "Sets the task to check. This corresonds to the title of the scheduled task, called TaskName in Powershell output."},
4447
"folder": {value: &l.Folder, description: "The folder where the scheduled task is saved. This is used for exact matches, unless recurisive option is enabled."},
4548
"recursive": {value: &l.Recursive, description: "Include the subfolders of the specified folder as well when searching for scheduled tasks."},
49+
"hidden": {value: &l.Hidden, description: "Include hidden tasks."},
4650
},
4751
defaultFilter: "enabled = true",
4852
defaultCritical: "exit_code < 0",
@@ -73,7 +77,7 @@ func (l *CheckTasksched) Build() *CheckData {
7377
{name: "next_run_time", description: "Time when the registered task is next scheduled to run", unit: UDate},
7478
{name: "parameters", description: "Last actions command line parameters"},
7579
{name: "execute", description: "Last actions executed program"},
76-
{name: "working_dir", description: "Last actions working directory"},
80+
{name: "working_directory", description: "Last actions working directory"},
7781
},
7882
exampleDefault: `
7983
check_tasksched

pkg/snclient/check_tasksched_windows.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,14 @@ func (l *CheckTasksched) addTasks(ctx context.Context, snc *Agent, check *CheckD
2929
}
3030
}
3131

32+
// Remove backslash at the end of the folder path, if it is not exactly root: "\"
33+
// "\Microsoft\" -> "\Microsoft"
34+
if l.Folder != CheckTaskschedDefaultFolder && l.Folder != "\\" {
35+
if cut, cutOk := strings.CutSuffix(l.Folder, "\\"); cutOk {
36+
l.Folder = cut
37+
}
38+
}
39+
3240
titleRuneBlacklist := []rune{'\\', '/', ':', '*', '?', '"', '<', '>', '|'}
3341
if l.TaskTitle != CheckTaskschedDefaultTaskTitle {
3442
if strings.ContainsFunc(l.TaskTitle, func(r rune) bool { return slices.Contains(titleRuneBlacklist, r) }) {
@@ -70,6 +78,14 @@ func (l *CheckTasksched) addTasks(ctx context.Context, snc *Agent, check *CheckD
7078
specifyValue: true,
7179
specifiedValue: strconv.FormatBool(l.Recursive),
7280
},
81+
PowerShellParameter{
82+
name: "hidden",
83+
parameterType: "string",
84+
specifyDefaultValue: true,
85+
defaultValue: strconv.FormatBool(CheckTaskschedDefaultHidden),
86+
specifyValue: true,
87+
specifiedValue: strconv.FormatBool(l.Hidden),
88+
},
7389
)
7490
if err != nil {
7591
return fmt.Errorf("error when building a powershell command: %s", err.Error())
@@ -118,7 +134,7 @@ func (l *CheckTasksched) addTasks(ctx context.Context, snc *Agent, check *CheckD
118134
"next_run_time": fmt.Sprintf("%d", l.parseDate(task.NextRunTime).Unix()),
119135
"parameters": l.parseParameters(task.Actions),
120136
"execute": l.parseExecuteCmd(task.Actions),
121-
"working_dir": l.parseWorkingDir(task.Actions),
137+
"working_directory": l.parseWorkingDir(task.Actions),
122138
}
123139
check.listData = append(check.listData, entry)
124140
}

pkg/snclient/embed/scripts/windows/scheduled_tasks.ps1

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,25 @@ if ($args) {
2424
$i++
2525
continue
2626
}
27+
if ($args[$i] -eq '-hidden' -and $i + 1 -lt $args.Count) {
28+
$hidden = $args[$i + 1]
29+
$i++
30+
continue
31+
}
2732
}
2833
}
2934

3035
# Apply defaults when variables are not defined (neither by snclient parameter injection nor by args)
3136
if (!$title) { $title = '*' }
3237
if (!$folder) { $folder = '\' }
3338
if (!$recursive) { $recursive = 'true' }
39+
if (!$hidden) { $hidden = 'true' }
40+
41+
# debug the parameters/arguments
42+
[Console]::Error.WriteLine(('title: ' + $title ))
43+
[Console]::Error.WriteLine(('folder: ' + $folder ))
44+
[Console]::Error.WriteLine(('recursive: ' + $recursive ))
45+
[Console]::Error.WriteLine(('hidden: ' + $hidden ))
3446

3547
# ensure output is utf8
3648
$OutputEncoding = [Console]::OutputEncoding = [Text.UTF8Encoding]::UTF8
@@ -54,7 +66,12 @@ try {
5466
$currentFolder = $folderQueue.Dequeue()
5567
# TASK_ENUM_HIDDEN = 1, include hidden tasks
5668
# Call GetTasks() using TASK_ENUM_HIDDEN
57-
foreach ($t in $currentFolder.GetTasks(1)) {
69+
if ($hidden -eq 'true'){
70+
$getTasksArg = 1
71+
} else {
72+
$getTasksArg = 0
73+
}
74+
foreach ($t in $currentFolder.GetTasks($getTasksArg)) {
5875
$tasks.Add($t)
5976
}
6077
if ($recursive -eq 'true') {

0 commit comments

Comments
 (0)