Powershell to Monitor Services
Hello,
I have specific services that I would like to monitor on specific servers and send out alert if service is stopped. Below is a script that works for monitoring one service on a specific server.
Ideally, I would like to do this to monitor multiple services on multiple servers. Not all servic names are going to be same for all servers. I am trying to think how to do it. Any guidance would be appreciated.
#All Variables are defined below
$servername = ‘InternalServerName’
$ServiceName = ‘InternalServiceName’
$ServiceStatus = Get-Service -ComputerName “$servername” -Name “$ServiceName”
$ServiceState = $ServiceStatus.Status
#All Variables are defined above
# All functions are defined below
Function ServiceCheck
{
if
($ServiceStatus.Status -eq ‘Running’) {Write-Host ‘Running’}
Else
{Sendmail(“Alert Triggered”)}
}
Function sendMail ($message)
{
“Sending Email”
#SMTP server name
$smtpServer = “smtp.office365.com”
#Creating a Mail object
$msg = new-object Net.Mail.MailMessage
$emailPassword = “HiddenPassword”
$emailCredential = New-Object System.Net.NetworkCredential(“email address removed for privacy reasons”, $emailPassword)
#Creating SMTP server object
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$smtp.Port = 587
$smtp.EnableSSl = $true
$smtp.Credentials = $emailCredential
#Email structure
$msg.From = “email address removed for privacy reasons”
$msg.To.add(“email address removed for privacy reasons”)
$msg.subject = “Alert – $ServiceName on $servername is $ServiceState”
$msg.body = “$ServiceName on $servername is $ServiceState”
#Sending email
$smtp.Send($msg)
“Email Sent”
}
# All functions are defined Above
#All functions are called below
cls
ServiceCheck
#All functions are called above
Hello, I have specific services that I would like to monitor on specific servers and send out alert if service is stopped. Below is a script that works for monitoring one service on a specific server. Ideally, I would like to do this to monitor multiple services on multiple servers. Not all servic names are going to be same for all servers. I am trying to think how to do it. Any guidance would be appreciated. #All Variables are defined below
$servername = ‘InternalServerName’
$ServiceName = ‘InternalServiceName’
$ServiceStatus = Get-Service -ComputerName “$servername” -Name “$ServiceName”
$ServiceState = $ServiceStatus.Status
#All Variables are defined above
# All functions are defined below
Function ServiceCheck
{
if
($ServiceStatus.Status -eq ‘Running’) {Write-Host ‘Running’}
Else
{Sendmail(“Alert Triggered”)}
}
Function sendMail ($message)
{
“Sending Email”
#SMTP server name
$smtpServer = “smtp.office365.com”
#Creating a Mail object
$msg = new-object Net.Mail.MailMessage
$emailPassword = “HiddenPassword”
$emailCredential = New-Object System.Net.NetworkCredential(“email address removed for privacy reasons”, $emailPassword)
#Creating SMTP server object
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$smtp.Port = 587
$smtp.EnableSSl = $true
$smtp.Credentials = $emailCredential
#Email structure
$msg.From = “email address removed for privacy reasons”
$msg.To.add(“email address removed for privacy reasons”)
$msg.subject = “Alert – $ServiceName on $servername is $ServiceState”
$msg.body = “$ServiceName on $servername is $ServiceState”
#Sending email
$smtp.Send($msg)
“Email Sent”
}
# All functions are defined Above
#All functions are called below
cls
ServiceCheck
#All functions are called above Read More