forked from Iakim/Zabbix-ScheduledTask
-
Notifications
You must be signed in to change notification settings - Fork 3
/
DiscoverScheduledTasks.ps1
85 lines (74 loc) · 3.5 KB
/
DiscoverScheduledTasks.ps1
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
# Script: DiscoverSchelduledTasks
# Author: Romain Si
# Revision: Isaac de Moraes
# Revision: Patrik Svestka
#
# This script is intended for use with Zabbix > 3.x
# Note: This script uses front slash in the path instead of Task Schedule's backslash path (XML) as the backslash are considered unsafe character in Zabbix
#
# Add to Zabbix Agent
# UserParameter=TaskSchedulerMonitoring[*],powershell -NoProfile -ExecutionPolicy Bypass -File "C:\Program Files\Zabbix Agent\DiscoverScheduledTasks.ps1" "$1" "$2" "$3"
#
# Note: should there be a need to use wild char star (*) in the task path - it is considered to be an unsafe character in Zabbix- it needs to be enabled
# in zabbix_agent2.conf using UnsafeUserParameters=1
#
# Examples when running directly from PowerShell:
#
# 1) List all tasks at the path /Accounting/ including the subfolders and list the /backup/ only
# `.\DiscoverSchelduledTasks.ps1 '/Accounting/*,/Backup/' 'DiscoverTasks'`
# 2) Show the last task result for the AccoungDailyBackup task
# `'.\DiscoverSchelduledTasks.ps1 '/Accounting/' 'TaskLastResult' 'AccountingDailyBackup'`
$ErrorActionPreference = "Stop"
function Convert-ToUnixDate($taskTime) {
$epoch = [timezone]::CurrentTimeZone.ToLocalTime([datetime]'1/1/1970')
if ($taskTime -eq $Null) { $taskTime = $epoch }
return (New-TimeSpan -Start $epoch -End $taskTime).TotalSeconds
}
# Note: Not using Get-ScheduledTaskInfo directly as it can not deal correctly with accented or wild characters
function Get-ScheduledTaskInfoByFullName($path, $name) {
return (Get-ScheduledTask -TaskPath "$path" -TaskName "$name" | Get-ScheduledTaskInfo)
}
function Get-ScheduledTaskByFullName($path, $name) {
return Get-ScheduledTask -TaskPath "$path" -TaskName "$name"
}
# Zabbix template is using the forward slash (/) instead of the Task schedule XML's backslash (\) - changing to the proper XML's style
$taskPath = ([string]$args[0]).replace('/','\')
$taskAction = [string]$args[1]
# getting only portion task name from the task path and name
$taskName = ([string]$args[2]).Substring(([string]$args[2]).LastIndexOf('/') + 1)
switch ($taskAction) {
'DiscoverTasks' {
$data = @()
$taskPath = $taskPath -split ',' | % { $_.Trim() }
foreach ($path in $taskPath) {
$appTasks = Get-ScheduledTask -TaskPath $path | where { $_.state -ne "Disabled" }
if ($appTasks -eq $null) { continue }
foreach ($currentAppTasks in $appTasks) {
$data += @{ '{#APPTASKS}' = $currentAppTasks.TaskPath.replace('\','/') + $currentAppTasks.TaskName }
}
}
$json = @{ 'data' = $data }
$json | ConvertTo-Json | Write-Host
}
'TaskLastResult' {
$lastTaskResult = (Get-ScheduledTaskInfoByFullName $taskPath $taskName).LastTaskResult
Write-Output $lastTaskResult
}
'TaskLastRunTime' {
$lastRunTime = (Get-ScheduledTaskInfoByFullName $taskPath $taskName).LastRunTime
$unixTime = Convert-ToUnixDate($lastRunTime)
Write-Output $unixTime
}
'TaskNextRunTime' {
$nextRunTime = (Get-ScheduledTaskInfoByFullName $taskPath $taskName).NextRunTime
$unixTime = Convert-ToUnixDate($nextRunTime)
Write-Output $unixTime
}
'TaskState' {
$taskState = (Get-ScheduledTaskByFullName $taskPath $taskName).State
Write-Output $taskState
}
Default {
throw "Error trying getting task action: $taskAction"
}
}