Friday, March 1, 2013

Script to check the drive space in the server against the threshold value and triggers an alert

Script to check Disk Space
#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 -path .\list.txt #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,Please cleanup: $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 File Server Owners
function SendEmail([string]$msg)
{
 $SMTPClient = new-object System.Net.Mail.smtpClient;
 $SMTPClient.host = "smtp.Domain.com"
 $MailMessage = new-object System.Net.Mail.MailMessage;
 $MailMessage.Subject = $emailsubject;
 $MailMessage.Body = $msg;
 $MailMessage.From = "
SCOMN_No_Reply@Domain.com";
 $MailMessage.To.add("
ToJhon@Domain.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);
}

Source:- http://www.sharepointdiary.com/2012/11/drive-space-monitoring-using-powershell.html

No comments:

Post a Comment