# encoding: ascii
# api: powershell
# title: Check for Service Outage
# description: This script checks for any service that is not running that needs to be running. This script creates a temp file with a list of the services that are not running and when this is detected as having values the script imports the entries from the .csv and emails them to the appropriate individuals. This script was designed to be ran repeatedly as a scheduled task.
# version: 0.1
# type: function
# author: David
# license: CC0
# x-poshcode-id: 5902
# x-archived: 2016-09-09T00:44:40
# x-published: 2016-06-19T22:42:00
#
#
function RunSendEmail
{
	$date = Get-Date -Format F
    Send-MailMessage -To "HelpDesk <helpdesk@company.com>" -cc "IT Tech #1 <ittech@company.com>" -From "Server Service Monitor <no-reply-services@company.com>" -Subject "[SERVER.company.com] Service Failure(s)" -Body "Report generated on: $date
    
    $NewBody" -SmtpServer EMAILSERVER
}

#Filters the types of services you want to check.
Get-WmiObject Win32_Service | Where-Object {$_.Name -like '*Backup*' -and $_.StartMode -eq 'Auto' -and $_.State -eq 'Stopped'} | Select-Object Name | export-csv C:\temp\services.csv -NoTypeInformation
Get-WmiObject Win32_Service | Where-Object {$_.Name -like '*DLO*' -and $_.StartMode -eq 'Auto' -and $_.State -eq 'Stopped'} | Select-Object Name | export-csv C:\temp\services.csv -NoTypeInformation -Append
Get-WmiObject Win32_Service | Where-Object {$_.Name -like '*BKUPEXEC*' -and $_.StartMode -eq 'Auto' -and $_.State -eq 'Stopped'} | Select-Object Name | export-csv C:\temp\services.csv -NoTypeInformation -Append

#Logic. If the file is not empty then there are failures.
$NewBody = "The following list are services found that are listed as to be automatically running`f"
$NewBody = $NewBody + "and are currently stopped / not running.`f`f"
$NewBody = $NewBody + "Failed Services:`f"
$test = Import-CSV C:\temp\services.csv
if ($test -ne $NULL)
{
	Foreach ($line in $test)
	{
		$NewLine = $line.Name
		$NewLine = $NewLine + "`f"
		$NewBody = $NewBody + $NewLine

	}
	RunSendEmail
}
