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
 

 

Friday, October 30, 2015

Add SharePoint Security Group to All Lists or Libraries in a Site using PowerShell



This is sometimes very hectic for admins to add a new SharePoint Security group to a number of Lists and Libraries.

Using below code, it will be very easy to add a security group to all lists and libraries.

If you want to add security group to a specific type of library, you can add filter like the one below

if($spList.BaseTemplate -eq "10002")

For the information about base template follow the link


# Get Site
$spSite = Get-SPSite http://siteurl
# Get all web in current site
$spWebs = $spSite.AllWebs
# Set Permission level in variable
$PermissionLevel = "Read"
# Loop All Webs in site
foreach($web in $spWebs)
{
    # Get the Site Group from current Web which you want to add in Permissions for List/Library
       $spGroup = $web.SiteGroups["Visitors"]
    # Loop through all the lists/Libraries
       foreach($spList in $web.Lists)
       {
      
             
              # Break in Inheritance, this will allow you to add groups, otherwise it will not allow
              $spList.BreakRoleInheritance($true);
              # create Role Assignment  
              $roleAssignment = new-object Microsoft.SharePoint.SPRoleAssignment($spGroup)
              # Get the RoleDefinition for permission level
              $roleDefinition = $web.RoleDefinitions[$PermissionLevel];
              # Bind Role Definition
              $roleAssignment.RoleDefinitionBindings.Add($roleDefinition);
              # Add Role to List
              $spList.RoleAssignments.Add($roleAssignment)
              # Turn the Inheritance ON
              $spList.BreakRoleInheritance($false);
              # Update List
              $spList.Update();

             
       }
}

$spSite.Dispose()
$spWebs.Dispose()