Powershell Script to Monitor Disk Space

Put in the server details in a text file and place it under the same folder where the PowerShell script to monitor the disk space exist. Name the text file as "ServerList.txt"

The script triggers an alert email when the disk space is less than 30% of the original size and sends another email with the disk space details in attached CSV file. You can change the calculations which better suits your case.
 
Modify the From, to address and SMTP server details in the script.  
  1. $LogTime = Get-Date -Format yyyy-MM-dd_hh-mm  
  2. $LogFile = ".\SPServerDiskSpaceDetailsPatch-$LogTime.rtf"  
  3.  
  4. # Add SharePoint PowerShell Snapin  
  5. if ( (Get-PSSnapin -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null ) {  
  6.     Add-PSSnapin Microsoft.SharePoint.Powershell  
  7. }  
  8.   
  9. $scriptBase = split-path $SCRIPT:MyInvocation.MyCommand.Path -parent  
  10. Set-Location $scriptBase  
  11.   
  12. $OutputFile = $scriptBase + "\" + "SPServerSpaceDetails.csv"  
  13.  
  14. #Removing old output files  

  15. if (test-path $OutputFile)  
  16. {  
  17.    remove-item $OutputFile  
  18. }  
  19.  
  20. #Removing old log files  

  21. $FindRTFFile = Get-ChildItem $scriptBase\*.* -include *.rtf  
  22. if($FindRTFFile)  
  23. {  
  24.    foreach($file in $FindRTFFile)  
  25.    {  
  26.       remove-item $file  
  27.    }  
  28. }  

start-transcript $logfile 

  1. #Server list details  
  2.   
  3. $serverList = $scriptBase + "\" + "ServerList.txt";  
  4.     
  5. $output = $OutputFile  
  6.   
  7. "Computer Name" + "," + "Drive" + "," + "Size in (GB)" + "," + "Free Space in (GB)" + "," + "Critical (*)" | Out-File -Encoding Default -FilePath $Output;  
  8.   
  9. foreach($computer in get-content $serverList)  
  10. {  
  11.   
  12.    $drives = Get-WmiObject -ComputerName $computer Win32_LogicalDisk | Where-Object {$_.DriveType -eq 3}   
  13.   
  14.    foreach($drive in $drives)   
  15.   
  16.    {   
  17.   
  18.        $id = $drive.DeviceID   
  19.   
  20.        $size = [math]::round($drive.Size / 1073741824, 2)   
  21.   
  22.        $free = [math]::round($drive.FreeSpace  / 1073741824, 2)   
  23.   
  24.        $pct = [math]::round($free / $size, 2) * 100   
  25.   
  26.       if ($pct -lt 30)  
  27.   
  28.       {   
  29.          $pct = $pct.ToString() + "% *** "  
  30.   
  31.          $emailFrom = "[email protected]"  
  32.         # Use commas for multiple addresses  
  33.         $emailTo = "[email protected]"  
  34.         $subject = "URGENT!!!! " + $drive.DeviceID + " drive is running out of space in SharePoint server"  
  35.         $body = "Hi SharePoint Admin Team , `n `n   The " + $drive.DeviceID + " drive in " + $computer + " server is running out of space. Please take necessary actions. `n `nThanks, `nSharePoint DiskSpace Monitoring Script."  
  36.         $smtpServer = "SMTP Server address"  
  37.         $smtp = new-object Net.Mail.SmtpClient($smtpServer)  
  38.         $smtp.Send($emailFrom, $emailTo, $subject, $body)  
  39.   
  40.       }  
  41.   
  42.       else {  $pct = $pct.ToString() + " %" }  
  43.   
  44.       $computer + "," + $id + "," + $size + "," + $free + "," + $pct  | Out-File -Encoding Default  -Append -FilePath $Output;  
  45.   
  46.       $pct = 0    
  47.   
  48.    }  
  49.   
  50. }  
  51.   
  52. $EmailFrom = "[email protected]"  
  53. $EmailTo = "[email protected]"  
  54. $EmailSubject = "Available free space in SharePoint servers"  
  55. $emailbody = "Hi SharePoint Admin Team,  
Kindly find the attached free space details for the SharePoint servers.

Thanks,

SharePoint DiskSpace Monitoring Script."     

  1. #enter the SMTP server name or IP  
  2.     
  3. $SMTPServer = "SMTP server address"  
  4. #$SMTPAuthUsername = "username"  
  5. #$SMTPAuthPassword = "password"  
  6.   
  7. $emailattachment = $OutputFile  
  8.   
  9. function send_email {  
  10.    $mailmessage = New-Object system.net.mail.mailmessage   
  11.    $mailmessage.from = ($emailfrom)   
  12.    $mailmessage.To.add($emailto)  
  13.    $mailmessage.Subject = $emailsubject  
  14.    $mailmessage.Body = $emailbody  
  15.   
  16.    $attachment = New-Object System.Net.Mail.Attachment($emailattachment, 'text/plain')  
  17.    $mailmessage.Attachments.Add($attachment)  
  18.    #$mailmessage.IsBodyHTML = $true  
  19.    $SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 25)    
  20.    $SMTPClient.Credentials = New-Object System.Net.NetworkCredential("$SMTPAuthUsername""$SMTPAuthPassword")   
  21.    $SMTPClient.Send($mailmessage)  
  22. }  
  23.   
  24. send_email  
  25.   
  26. stop-transcript  
Next Recommended Reading Disk Cleanup Using Powershell Scripts