Wednesday, November 4, 2015

Get SharePoint Farm server status by Email using PowerShell

Admins are required to get information about SharePoint farm server in order to have a check if machines in farm are running properly. If a server is down or drive hosting the SharePoint sites or on database server is full, this could end up with down time on a specific SharePoint application or an entire farm could go offline.

using PowerShell script, you can get an email about the physical state of SharePoint farm.

This script can also be automated using Windows task scheduler, so that an email can be send daily at a specific time.

Add-PSSnapin Microsoft.SharePoint.PowerShell

Function GetServerDetail(){
# Get All the servers in a SharePoint Farm
$AllServers = GET-SPServer

# Create HTML Table and first row as header
$html = "<table  cellpadding='0'  cellspacing='0' style='width:800px;border:1px solid black;font-family:verdana;font-size:11px;'><tr>"
$html += "<td style='border:1px solid white;color:white;background-color:black; font-family:verdana;font-weight:bold;'>Server Name/IP</td>"
$html += "<td style='border:1px solid white;color:white;background-color:black; font-family:verdana;font-weight:bold;'>Server Role/IP</td>"
$html += "<td style='border:1px solid white;color:white;background-color:black; font-family:verdana;font-weight:bold;'>Online</td>"
$html += "<td style='border:1px solid white;color:white;background-color:black; font-family:verdana;font-weight:bold;'>C Drive total Space (GB)</td>"
$html += "<td style='border:1px solid white;color:white;background-color:black; font-family:verdana;font-weight:bold;'>C Drive free Space (GB)</td>"
$html += "<td style='border:1px solid white;color:white;background-color:black; font-family:verdana;font-weight:bold;'>D Drive total Space (GB)</td>"
$html += "<td style='border:1px solid white;color:white;background-color:black; font-family:verdana;font-weight:bold;'>D Drive free Space (GB)</td></tr>"

# Loop through all the servers in current SharePoint farm
foreach($server in $AllServers)
{
# Server list inclused smtp server, if mail settings are configured in the central admin. we will skip it here
if($server.Name -match "smtp")
{
continue
}
# Get the C Drive and D Drice information for current server.
$cdisk = Get-WmiObject Win32_LogicalDisk -ComputerName $Server.Name -Filter "DeviceID='C:'" | Foreach-Object {$_.Size,$_.FreeSpace} -ErrorAction SilentlyContinue
$ddisk = Get-WmiObject Win32_LogicalDisk -ComputerName $Server.Name -Filter "DeviceID='D:'" | Foreach-Object {$_.Size,$_.FreeSpace} -ErrorAction SilentlyContinue
# Check if server is online
$Status = Test-Connection -BufferSize 32 -Count 1 -ComputerName $server.Name -Quiet

# Create a row for every server information.

$html += "<tr><td style='border:1px solid black;'>" + $Server.Name +"</td>"
$html += "<td style='border:1px solid black;'>" + $Server.Role +"</td>"
$html += "<td style='border:1px solid black;'>" +  $Status +"</td>"
$html += "<td style='border:1px solid black;'>" +   [math]::round((($cdisk[0]/1024)/1024)/1024,1) +"</td>"
$html += "<td style='border:1px solid black;'>" +   [math]::round((($cdisk[1]/1024)/1024)/1024,1) +"</td>"
$html += "<td style='border:1px solid black;'>" +   [math]::round((($ddisk[0]/1024)/1024)/1024,1) +"</td>"
$html += "<td style='border:1px solid black;'>" +   [math]::round((($ddisk[1]/1024)/1024)/1024,1) +"</td></tr>"


}

# Close the HTML table
$html += "</table>";
$html

}

# Get all the information for E-Mail Body
$MailBody = GetServerDetail

# Send Email body
send-mailmessage -from "SharePoint 2013 <sharepoint2013@test.com>" -to "User01 <user01@test.com>" -subject "SharePoint Server Monitoring" -body $MailBody -BodyAsHtml -priority High -dno onSuccess, onFailure -smtpServer smtp.test.com

Following out put will be received in an email.

Server Name/IP
Server Role
Online
C Drive total Space (GB)
C Drive free Space (GB)
D Drive total Space (GB)
D Drive free Space (GB)
Server01
Application
True
99.7
35.1
50
8.4
Server02
Application
True
99.7
13.4
50
9.1
Server03
Invalid
True
99.7
63.4
452
136.9