forked from romainsi/zabbix-scheduledtask
-
Notifications
You must be signed in to change notification settings - Fork 9
/
DiscoverScheduledTasks.ps1
99 lines (84 loc) · 3.6 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# Script: DiscoverSchelduledTasks
# Author: Romain Si
# Revision: Isaac de Moraes
# This script is intended for use with Zabbix > 3.x
#
#
# Add to Zabbix Agent
# UserParameter=TaskSchedulerMonitoring[*],powershell -NoProfile -ExecutionPolicy Bypass -File "C:\Program Files\Zabbix Agent\DiscoverScheduledTasks.ps1" "$1" "$2"
#
## Modifier la variable $path pour indiquer les sous dossiers de Tâches Planifiées à traiter sous la forme "\nomDossier\","\nomdossier2\sousdossier\" voir (Get-ScheduledTask -TaskPath )
## Change the $path variable to indicate the Scheduled Tasks subfolder to be processed as "\nameFolder\","\nameFolder2\subfolder\" see (Get-ScheduledTask -TaskPath )
$path = "\"
Function Convert-ToUnixDate ($PSdate) {
$epoch = [timezone]::CurrentTimeZone.ToLocalTime([datetime]'1/1/1970')
(New-TimeSpan -Start $epoch -End $PSdate).TotalSeconds
}
$ITEM = [string]$args[0]
$ID = [string]$args[1]
switch ($ITEM) {
"DiscoverTasks" {
$apptasks = Get-ScheduledTask -TaskPath $path | where {$_.state -like "Ready" -and "Running"}
$apptasksok1 = $apptasks.TaskName
$apptasksok = $apptasksok1.replace('â','â').replace('à','à').replace('ç','ç').replace('é','é').replace('è','è').replace('ê','ê')
$idx = 1
write-host "{"
write-host " `"data`":[`n"
foreach ($currentapptasks in $apptasksok)
{
if ($idx -lt $apptasksok.count)
{
$line= "{ `"{#APPTASKS}`" : `"" + $currentapptasks + "`" },"
write-host $line
}
elseif ($idx -ge $apptasksok.count)
{
$line= "{ `"{#APPTASKS}`" : `"" + $currentapptasks + "`" }"
write-host $line
}
$idx++;
}
write-host
write-host " ]"
write-host "}"}}
switch ($ITEM) {
"TaskLastResult" {
[string] $name = $ID
$name1 = $name.replace('â','â').replace('à','à').replace('ç','ç').replace('é','é').replace('è','è').replace('ê','ê')
$pathtask = Get-ScheduledTask -TaskPath "*" -TaskName "$name1"
$pathtask1 = $pathtask.Taskpath
$taskResult = Get-ScheduledTaskInfo -TaskPath "$pathtask1" -TaskName "$name1"
Write-Output ($taskResult.LastTaskResult)
}}
switch ($ITEM) {
"TaskLastRunTime" {
[string] $name = $ID
$name1 = $name.replace('â','â').replace('à','à').replace('ç','ç').replace('é','é').replace('è','è').replace('ê','ê')
$pathtask = Get-ScheduledTask -TaskPath "*" -TaskName "$name1"
$pathtask1 = $pathtask.Taskpath
$taskResult = Get-ScheduledTaskInfo -TaskPath "$pathtask1" -TaskName "$name1"
$taskResult1 = $taskResult.LastRunTime
$date = get-date -date "01/01/1970"
$taskResult2 = Convert-ToUnixDate($taskResult1)
Write-Output ($taskResult2)
}}
switch ($ITEM) {
"TaskNextRunTime" {
[string] $name = $ID
$name1 = $name.replace('â','â').replace('à','à').replace('ç','ç').replace('é','é').replace('è','è').replace('ê','ê')
$pathtask = Get-ScheduledTask -TaskPath "*" -TaskName "$name1"
$pathtask1 = $pathtask.Taskpath
$taskResult = Get-ScheduledTaskInfo -TaskPath "$pathtask1" -TaskName "$name1"
$taskResult1 = $taskResult.NextRunTime
$date = get-date -date "01/01/1970"
$taskResult2 = Convert-ToUnixDate($taskResult1)
Write-Output ($taskResult2)
}}
switch ($ITEM) {
"TaskState" {
[string] $name = $ID
$name1 = $name.replace('â','â').replace('à','à').replace('ç','ç').replace('é','é').replace('è','è').replace('ê','ê')
$pathtask = Get-ScheduledTask -TaskPath "*" -TaskName "$name1"
$pathtask1 = $pathtask.State
Write-Output ($pathtask1)
}}