Skip to content

Instantly share code, notes, and snippets.

@wizact
Created December 14, 2012 14:34
Show Gist options
  • Save wizact/4285850 to your computer and use it in GitHub Desktop.
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.
# 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
}
@kumarravi3
Copy link

Thanks, this worked great. Exactly what I was looking for, short and simple 👍

@yogeshparkar
Copy link

This works for me as well thanks .................................:)

@Vasu-Reddy
Copy link

This really worked well. Thank you very much. I really appreciate your help!!.

@dipakmahadeo
Copy link

How to check with multiple services.

@dipakmahadeo
Copy link

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