We have a couple of clients who have these janky services that always seem to be having problems. I put this together over the weekend as my first powershell effort. I am certain there is a more elegant way of doing things. What do you guys think?
##############################################
#
#
#Powershell Monitoring Services
#
#
# written by some guy
#
# and
#
# his kool-aid
#
#
#
#
###############################################
$file = "C:\services.txt" # set file parameter from list of monitored services
$names = Get-Content $file # set names parameter, feeds names into command
Get-Service $names | Where-Object {$_.status -eq "stopped"} | Format-List | Out-file C:\powershell\psmonerrors.txt # Checking for dead services
$keyswitch = Get-ItemProperty -path C:\powershell\psmonerrors.txt -name "length"
####Below is email params
$emailfrom = "<alert address>"
$emailto= "<recipient address>"
$subject = "SERVICES STOPPED MESSAGE"
$body = Get-Content C:\powershell\psmonerrors.txt
$smtpserver = "<server address>"
$smtp = New-Object net.Mail.SmtpClient($smtpserver)
#If anything is dead, it extends the file length and triggers the email.
if ($keyswitch.length -gt 2) {$smtp.send($emailfrom, $emailto, $subject, $body)}
Get-Service $names | Where-Object {$_.status -eq "stopped"} | Start-Service #Attempts to restart the service