# encoding: ascii
# api: powershell
# title: Check Service on Servers
# description: If you download or use, please tweet it or email me. Thanks!
# version: 1.0
# type: script
# author: Matt Schmitt
# license: CC0
# x-poshcode-id: 3803
# x-archived: 2013-11-11T19:09:27
# x-published: 2013-11-29T12:46:00
#
# This project is in response to “script to monitor a service on a server” script request: http://gallery.technet.microsoft.com/scriptcenter/site/requests/script-to-monitor-a-service-on-a-server-636b6a0d?ShowSubmitLinkForm=False
# This script checks the status for a defiled service for a group of servers, listed in the serverList.txt file. In this case the CPSVS service. However this can be changed to the name of the service you want to check. It exports the status of the service for each server. After that, it sends the output file as an attachment in an email to a defined group of addresses.
# To use this script your will need to make some changes in the scritp:
# Create Server list and save to serverList.txt file
# Adjust the import location of the serverList.txt file on Line 15
# Change the service – to the service you want to check
# Adjust the output location of the ServerServiceStatus.csv file on line 27
# Adjust the email address on Line 32.
# To use more than one email address, separtate with a comma:
# recipient1@domain.com, recipient2@domain.com
# Adjust the SmtpServer address on Line 32
# # I hope you find this useful! Comments and suggestions are welcome!
# Matt
#
<#
Author: Matt Schmitt
Date: 11/29/12
Version: 1.0
From: USA
Email: ithink2020@gmail.com
Website: http://about.me/schmittmatt
Twitter: @MatthewASchmitt
Description
A script for checking the status of a service on a group of servers, from a list in a file.
#>
$serverList = Import-Csv 'c:\serverList.csv'
"Server" +"`t" + "Status" | Out-File c:\ServerService.csv
foreach ($element in $serverList)
{
$sStatus = get-service -Name "CPSVS" | Select-Object -expand Status
$server = $element | Select-Object -expand Server
$server + "`t" + $sStatus | Out-File -append c:\ServerServiceStatus.csv
}
Send-MailMessage -From donotreply@test.com -To recipient@domain.com -subject "Spooler Service Report" -Body "Attached is Server Service report." -Attachments "c:\ServerServiceStatus.csv" -SmtpServer "xxx.xxx.xxx.xxx"