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. # Add SharePoint PowerShell Snapin
  4. if ( (Get-PSSnapin -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null ) {
  5. Add-PSSnapin Microsoft.SharePoint.Powershell
  6. }
  7. $scriptBase = split-path $SCRIPT:MyInvocation.MyCommand.Path -parent
  8. Set-Location $scriptBase
  9. $OutputFile = $scriptBase + "\" + "SPServerSpaceDetails.csv"
  10. #Removing old output files

  11. if (test-path $OutputFile)
  12. {
  13. remove-item $OutputFile
  14. }
  15. #Removing old log files

  16. $FindRTFFile = Get-ChildItem $scriptBase\*.* -include *.rtf
  17. if($FindRTFFile)
  18. {
  19. foreach($file in $FindRTFFile)
  20. {
  21. remove-item $file
  22. }
  23. }

start-transcript $logfile

  1. #Server list details
  2. $serverList = $scriptBase + "\" + "ServerList.txt";
  3. $output = $OutputFile
  4. "Computer Name" + "," + "Drive" + "," + "Size in (GB)" + "," + "Free Space in (GB)" + "," + "Critical (*)" | Out-File -Encoding Default -FilePath $Output;
  5. foreach($computer in get-content $serverList)
  6. {
  7. $drives = Get-WmiObject -ComputerName $computer Win32_LogicalDisk | Where-Object {$_.DriveType -eq 3}
  8. foreach($drive in $drives)
  9. {
  10. $id = $drive.DeviceID
  11. $size = [math]::round($drive.Size / 1073741824, 2)
  12. $free = [math]::round($drive.FreeSpace / 1073741824, 2)
  13. $pct = [math]::round($free / $size, 2) * 100
  14. if ($pct -lt 30)
  15. {
  16. $pct = $pct.ToString() + "% *** "
  17. $emailFrom = "[email protected]"
  18. # Use commas for multiple addresses
  19. $emailTo = "[email protected]"
  20. $subject = "URGENT!!!! " + $drive.DeviceID + " drive is running out of space in SharePoint server"
  21. $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."
  22. $smtpServer = "SMTP Server address"
  23. $smtp = new-object Net.Mail.SmtpClient($smtpServer)
  24. $smtp.Send($emailFrom, $emailTo, $subject, $body)
  25. }
  26. else { $pct = $pct.ToString() + " %" }
  27. $computer + "," + $id + "," + $size + "," + $free + "," + $pct | Out-File -Encoding Default -Append -FilePath $Output;
  28. $pct = 0
  29. }
  30. }
  31. $EmailFrom = "[email protected]"
  32. $EmailTo = "[email protected]"
  33. $EmailSubject = "Available free space in SharePoint servers"
  34. $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. $SMTPServer = "SMTP server address"
  3. #$SMTPAuthUsername = "username"
  4. #$SMTPAuthPassword = "password"
  5. $emailattachment = $OutputFile
  6. function send_email {
  7. $mailmessage = New-Object system.net.mail.mailmessage
  8. $mailmessage.from = ($emailfrom)
  9. $mailmessage.To.add($emailto)
  10. $mailmessage.Subject = $emailsubject
  11. $mailmessage.Body = $emailbody
  12. $attachment = New-Object System.Net.Mail.Attachment($emailattachment, 'text/plain')
  13. $mailmessage.Attachments.Add($attachment)
  14. #$mailmessage.IsBodyHTML = $true
  15. $SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 25)
  16. $SMTPClient.Credentials = New-Object System.Net.NetworkCredential("$SMTPAuthUsername", "$SMTPAuthPassword")
  17. $SMTPClient.Send($mailmessage)
  18. }
  19. send_email
  20. stop-transcript