Wednesday, November 14, 2012

Drive Space Monitoring using PowerShell Script

In SharePoint servers monitoring disk space is a common administrator task. Because, SharePoint servers may run out of disk space from SharePoint, IIS Logs, SQL Server transaction logs or even from SharePoint content (If you don't have Quotas enabled!)

There are lot of first-party, third-party utilities available, such as SCOM to Monitor disk space. What if you don't have such tools in your environment? no worries, Just PowerShell can do the monitoring of disk space! PowerShell disk space monitoring can be a cost saver too!

Schedule this PowerShell script in windows task scheduler("powershell.exe D:\scripts\DriveSpaceMonitor.ps1" . Make sure PowerShell is in your "Path" environment variable!) on hourly basis. Script will run every hour and mail only when the drive's free space goes below the provided threshold value.

Here is the PowerShell script for disk space monitoring:

#Purpose: Script to check the drive space in the server against the threshold value and triggers an alert
#powershell script for disk space monitoring 
$computer = get-content env:computername; #Get the server name
$percentWarning = 50; # Store the percentage warning threshold
$disks = Get-WmiObject -ComputerName $computer -Class Win32_LogicalDisk -Filter "DriveType = 3";

$mailbody = "The following drive(s) have less than $percentWarning% free sapce in Server: $computer`n`n";

$mailbody+="Drive`t`tTotalspace(GB)`t`t`Freespace(GB)`n --------------------------------------------------------------- `n";
$drivedata="";
$emailsubject="$computer - Low Disk Space Alert!";


# Code to send email to the SharePoint Administrator/Group
function SendEmail([string]$msg)
{
 $SMTPClient = new-object System.Net.Mail.smtpClient;
 $SMTPClient.host = "smtp.yourcompany.org"
 $MailMessage = new-object System.Net.Mail.MailMessage;
 $MailMessage.Subject = $emailsubject;
 $MailMessage.Body = $msg;
 $MailMessage.From = "DiskSpaceMonitor@crescent.com";
 $MailMessage.To.add("salaudeen.rajack@crescent.com");
 $SMTPClient.Send($MailMessage);
}

#The following block performs the core functionality
foreach($disk in $disks) 
{ 
 $deviceID = $disk.DeviceID; 
 [float]$size = $disk.Size; 
 [float]$freespace = $disk.FreeSpace;   
 $percentFree = [Math]::Round(($freespace / $size) * 100, 2); 
 $sizeGB = [Math]::Round($size / 1073741824, 2); 
 $freeSpaceGB = [Math]::Round($freespace / 1073741824, 2);   
 if($percentFree -lt $percentWarning) 
 { 
  $drivedata += "$deviceID`t`t $sizeGB`t`t`t$freeSpaceGB`n";

 } 
}

#Email to be sent only if any drive has free space less than threshold value
if($drivedata.length -gt 0)
{
 $msg=$mailbody+$drivedata;
 SendEmail($msg);
}

and see it in action! (I've changed the threshold to 50% for testing. It can be 10%-20% in general)

disk space powershell report
monitoring disk space with powershell
If you want to monitor for multiple servers, just use this trick:
#Array to hold list of Servers
$computers = @("ServerName1","ServerName2","ServerName3","ServerName4")

#Iterate through each server
foreach ($computer in $computers)
{
   #Place the above code inside
}
Alternatively, To get all SharePoint Servers, use:
#Get all SharePoint Servers
$servers = Get-SPServer | where { $_.role -ne "Invalid"}

foreach($computer in $servers)
{
    write-host $Computer.Name
}

How to Add Attachment:
#Attachment
$attachmentPath = "C:\Reports\MonthlyRpt.zip"

$Attachment = new-object System.Net.Mail.Attachment($attachmentPath) 
$MailMessage.Attachments.Add($Attachment) 

Tail: SharePoint 2010's Health analyzer has the rule to check "Drives are running out of free space." which can be utilized as well. But by default SharePoint health analyzer won't send email notifications and only display to users within Central Administration.



You might also like:
SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Document SharePoint Farm
Automatically generate SharePoint documentation.
*Sponsored


Check out these SharePoint products:

1 comment :

  1. It's amazing how Powershell is able to do the functions supposed to be handled by first-party and third-party utilities. I learned a lot from this post. Keep it up.

    ReplyDelete

Please Login and comment to get your questions answered!

You might also like:

Related Posts Plugin for WordPress, Blogger...