-
Notifications
You must be signed in to change notification settings - Fork 6
/
Powershell - Service monitoring script
81 lines (52 loc) · 2.64 KB
/
Powershell - Service monitoring script
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
### Windows Powershell Service Monitoring Script ####
###Features
## 1. Graphical message box
## 2. Sleep enabled
## 3. Email enabled
## 4. Continous monitoring enabled
## 5. This script cannot be stopped
############## For .Net Graphical message box
Add-Type -AssemblyName PresentationCore,PresentationFramework
################################ EMAIL INFORMATIONS ################################################################################
$username = "mani@paylin.com"
$passwd = Get-Content 'C:\Payilagam\secured.txt'
$ps = ConvertTo-SecureString -AsPlainText $passwd -Force
$SecureString = $ps
$MySecureCreds = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $username,$SecureString
$From = "mani@paylin.com"
$To = "muthu@paylin.com"
$Cc = "mani@paylin.com"
$Attachment = "C:\Payilagam\Spoolerlog.txt"
$Subject1 = "Critical Status - Spooler Service Stopped Alert"
$Subject2 = "Normal Status - Spooler Service Started"
$Body1 = "Hello,
This email is generated from the powershell script.
You are receiving this email because of the Spooler service in VM - 192.168.20.30 has been unfortunately stopped.
kindly find the attached log file in order to know the reason"
$Body2 = "Hello,
This email is generated from powershell script.
You are receiving this email because of the Spooler service in VM - 192.168.20.30 has been successfully started."
$SMTPServer = "192.168.1.30"
$SMTPPort = "25“
#######################################################################################################################################
$val = "Start"
while ($val -ne "Stop")
{
################### Selecting our service
$svs = Get-Service | WHERE {$_.Name -eq "spooler"}
################# Condition - If our selected service got stopped
if($svs.Status -eq "Stopped"){
Get-EventLog system -newest 10 | Format-List >> C:\Payilagam\Spoolerlog.txt
Send-MailMessage -From $From -to $To -Cc $Cc -Subject $Subject1 -Body $Body1 -SmtpServer $SMTPServer -port $SMTPPort -Credential $MySecureCreds -Attachments $Attachment
Start-Service $svs
if($svs.Status -eq "Running"){
$MessageboxTitle = “Spooler Service - Alert”
$Messageboxbody = “Spooler service got stopped and started successfully. Email Alert has been sent to support@domain.com”
$MessageIcon = [System.Windows.MessageBoxImage]::Warning
$ButtonType = [System.Windows.MessageBoxButton]::Ok
Send-MailMessage -From $From -to $To -Cc $Cc -Subject $Subject2 -Body $Body2 -SmtpServer $SMTPServer -port $SMTPPort -Credential $MySecureCreds
$Result1 = [System.Windows.MessageBox]::Show($Messageboxbody,$MessageboxTitle,$ButtonType,$MessageIcon)
start-sleep -seconds 60
}
}
}