# Script by N.I
#This script checks the status of a specified service, restarts it if it is not running, and sends an email notification upon completion.
#Settings
#----------------------------
$From = "CHANGEIT"
$sendTo = "CHANGEIT"
$SMTPServer = "CHANGEIT"
#Replace the Services EX: $ServicesTocheck = "Bonjour Service","DusmSvc"
$ServicesTocheck = "Bonjour Service","DusmSvc"
$ServerName = "CHANGEIT"
#--------------------------------
#Receive the name of the service and verify it's running
function FuncCheckService{
param($ServiceName)
$arrService = Get-Service -Name $ServiceName
if ($arrService.Status -ne "Running"){
Start-Service $ServiceName
$serviceName=$serviceName -replace(' ','
')
FuncMail -To $sendTo -From $From -Subject "$ServerName : Service stopped." -Body "The following services were stopped
$ServiceName
Services Started" -smtpServer $SMTPServer
}
}
function FuncMail {
param($To, $From, $Subject, $Body, $smtpServer)
$msg = new-object Net.Mail.MailMessage
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$msg.From = $From
$msg.To.Add($To)
$msg.Subject = $Subject
$msg.IsBodyHtml = 1
$msg.Body = $Body
$smtp.Send($msg)
}
#Send the name of the service to the function Check Service
FuncCheckService -ServiceName $ServicesTocheck