As a SharePoint admin, the most important role calls for monitoring servers and maintain the SLA’s. I guess there will be no second thoughts on this. For an admin as the environment grows to multiple farms and servers at different location it calls for automation – mostly on routine tasks to save time for their personal life and here in I am attempting to address how it did saved me some time. Automate monitor/start SharePoint Services on farm servers. You can script it to check these services at every restart and if you are the unlucky one where some of the services keep on stopping or unstable you can schedule the the script to check the services and attempt to start them and also email you the status.
So, we are looking at the script to:
- Check the services at startup or on scheduled time.
- Attempt to start the services if they are stopped
- Send an email with the status of the services from each server.
What you need to know before trying this option: SMTP server or mailhost server (which has to be unauthenticated SMTP).
I had this script, apparently it is been pieced together for my requirements and then scheduled to run on each reboot. How to call this script, 2 files created: Batch file which refers the PowerShell script (keep both these files in same path)
- servicescheck.bat
@echo off
PUSHD "%~dp0"
powershell -file "servicescheck.ps1" < NUL
exit
- Servicescheck.ps1
param
(
[Parameter(Mandatory=$false, HelpMessage='-ServiceNames Optional, provide a set of service names to restart.')]
[Array]$ServiceNames=@("SharePoint 2010 Tracing","Simple Mail Transfer Protocol (SMTP)","SharePoint 2010 Timer","SharePoint 2010 Administration","IIS Admin Service","World Wide Web Publishing Service", "Net.Tcp Listener Adapter","Net.Pipe Listener Adapter")
);
$server = hostname;
$emailbody = "";
Write-Host "Attempting to start services on" $server -ForegroundColor White;
foreach($serviceName in $ServiceNames)
{
$serviceInstance = Get-Service -DisplayName $serviceName -ErrorAction SilentlyContinue;
if(($serviceInstance -ne $null) -AND ($serviceInstance.Status -eq "Stopped"))
{
# Write-Host "Attempting to start service" $serviceName ".." -ForegroundColor White -NoNewline;
try
{
start-Service -InputObject $serviceInstance;
}
catch
{
Write-Output "Error Occured: " $_.Message;
}
}
}
$emailbody = foreach($servicename in $servicenames){ Get-Service $servicename -ErrorAction SilentlyContinue | Select-Object Status, DisplayName | ConvertTo-Html -Fragment}
$users = "Venu Madhav <VenuMadhav@outlook.com>"
$fromemail = "$server@Contoso.com"
$smtpserver = "mailhost.contoso.com"
# assemble the HTML for our body of the email report.
$HTMLmessage = @"
$emailbody
"@
send-mailmessage -From $fromemail -to $users -subject "$server Rebooted - Services Status" -BodyAsHTML -Body $HTMLmessage -priority High -smtpServer $smtpserver
$emailbody = "";
exit
Tags: Monitor Services, Automate Service Monitoring, PowerShell, SharePoint, Automate monitoring SharePoint and Windows Services