Tuesday, August 19, 2014

Drive Space Monitoring using PowerShell Script


#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); } Read more: http://www.sharepointdiary.com/2012/11/drive-space-monitoring-using-powershell.html#ixzz3AuUxN6qo

No comments:

Post a Comment

Google+ Followers