This repository was archived by the owner on Feb 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTaskmon.ps1
More file actions
88 lines (74 loc) · 2.97 KB
/
Taskmon.ps1
File metadata and controls
88 lines (74 loc) · 2.97 KB
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
##################################################################################
# Author:Iuri Jacob
# Date: 2019-03-19
# modified: 2019-04-04
# Description: Script to monitor windows scheduled tasks.
# It checks for tasks failures in the last minutes and sends
# e-mails alerts if such tasks exists.
##################################################################################
## Settings
$checkperiod = "-60" # Last minutes to check for task failures (must be negative)
$matchtitle = "MyTask" # Only tasks which title contains this string will be monitored
$matchfolder = "\MonitoredTasks" # Only tasks within this folder will be monitored
$emailto = "monitor@mytask.com" # sender e-mail
$emailfrom = "monitor@mytask.com" # receivers e-mail
$smtpserver ="smtp.mytask.com"
$smtpport="587"
$smtpuser="user@mytask.com"
$smtppassword="mytask password"
# current date/time strings
$nowdate = get-date -format d
$nowtime = get-date -format t
$nowdate = $nowdate.ToString().Replace("/", "-")
$nowtime = $nowtime.ToString().Replace(":", "-")
$nowtime = $nowtime.ToString().Replace(" ", "")
# Log files
$pwlogs = ".\Logs" + "\" + "Powershell" + $nowdate + "_" + $nowtime + "_.txt"
$maillogs = ".\Logs" + "\" + "emailcheck" + $nowdate + "_.txt"
Start-Transcript -Path $pwlogs
if((test-path $maillogs) -like $false)
{
new-item $maillogs -type file
}
# Get the server list
$serverlist= Get-Content .\servers.txt
foreach ($server in $serverlist)
{
$schedule = new-object -com("Schedule.Service")
$schedule.connect("$server")
$tasks = $schedule.getfolder(matchfolder).gettasks(0)
# Get all tasks that contains $matchtitle in the title and has any failure in the last $checkperiod minutes
$failures = $tasks | where-object{($_.LastTaskResult -ne 0) -and ($_.State -eq 3) -and ($_.Name -match $matchtitle) -and ((get-date).addminutes($checkperiod) -lt $_.LastRunTime)}
if ($failures -eq $Null)
{
write-host "No task failed in the last minutes in the server $server"
}
else
{
# Sends an e-mail for each task found
foreach ($task in $failures)
{
$b = $null > ".\Failures.txt"
$task >> ".\Failures.txt"
$taskdata = [IO.File]::ReadAllText($pwd.Path+"\Failures.txt")
$taskskname = $task.Name
$lastrun = $task.LastRunTime
write-host "The task " + $taskskname + " have failed recently in server " + $server
$subject ="[TaskMonitor][Failure]" + " - [" +$server + "]." + $taskskname + " "+" Last run: " + $lastrun
write-host "$subject" -ForegroundColor green
add-content $maillogs $subject
$body = $taskdata
$message = new-object Net.Mail.MailMessage
$smtp = new-object Net.Mail.SmtpClient($smtpserver, $smtpport)
$smtp.Credentials = New-Object System.Net.NetworkCredential("$smtpuser", "$smtppassword")
$message.From = $emailfrom
$message.To.Add($emailto)
$message.body = $body
$message.subject = $subject
$message.IsBodyHTML = $false
$smtp.Send($message)
$message.dispose()
}
}
}
Stop-Transcript