Created
December 14, 2012 14:34
-
-
Save wizact/4285850 to your computer and use it in GitHub Desktop.
PowerShell script to check the status of a windows service, send an alert and start it if it is stopped.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Created by: Amir | |
# Date: 14 December 2012 | |
# Description: Check Win Service to see if it is running and start it otherwise | |
# Usage: powershell -Command "C:\Scripts\serviceMonitor.ps1 | Out-File C:\Scripts\ServiceMonitor.txt -append" | |
$serviceName = "SampleServiceName" | |
$service = Get-Service | Where-Object { $_.Name -eq $serviceName } | |
write-host "Service Status is" $service.status | |
if ($service.status -eq "Stopped") | |
{ | |
write-host "Sending waring email" | |
$smtpServer = "smtp.server.name" | |
$msg = new-object Net.Mail.MailMessage | |
$smtp = new-object Net.Mail.SmtpClient($smtpServer) | |
$dateString = get-date | |
$msg.From = "noreply@example.com" | |
$msg.To.Add("amir@example.com") | |
$msg.subject = "Service " + $service + " is " + $service.status | |
$msg.body = ([string]$dateString) + " Service " + $service + " is " + $service.status | |
$smtp.send($msg) | |
#Starting the service | |
Start-Service $serviceName | |
} |
This works for me as well thanks .................................:)
This really worked well. Thank you very much. I really appreciate your help!!.
How to check with multiple services.
Can someone help me with few modification where script check series of windows services status and restart the services If its not running? I would also like to send one consolidated email.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks, this worked great. Exactly what I was looking for, short and simple 👍