# encoding: utf-8
# api: powershell
# title: Check Service
# description: Use to check a service on a remote server and if it is not running, start it and send an email to warn. Added a ping request to make sure the server is up before checking the service.
# version: 0.1
# type: script
# author: John Kroes
# license: CC0
# x-poshcode-id: 1229
# x-archived: 2012-04-03T18:09:42
# x-published: 2012-07-23T15:31:00
#
#
####################################################################################
#PoSH script to check if a server is up and if it is check for a service.
#If the service isn't running, start it and send an email
# JK - 7/2009
#Updated: 07/22/09 by Steven Murawski (http://blog.usepowershell.com)
#Changed the ping result comparison to use the .NET Enum
####################################################################################
$erroractionpreference = "SilentlyContinue"
$i = "testserver" #Server Name
$service = "spooler" #Service to monitor
$ping = new-object System.Net.NetworkInformation.Ping
$rslt = $ping.send($i)
if ($rslt.status �eq [System.Net.NetworkInformation.IPStatus]::Success)
{
$b = get-wmiobject win32_service -computername $i -Filter "Name = '$service'"
If ($b.state -eq "stopped")
{
$b.startservice()
$emailFrom = "services@yourdomain.com"
$emailTo = "you@yourdomain.com"
$subject = "$service Service has restarted on $i"
$body = "The $service service on $i has crashed and been restarted"
$smtpServer = "xx.yourdomain.com"
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$smtp.Send($emailFrom, $emailTo, $subject, $body)
}
}