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.
- $LogTime = Get-Date -Format yyyy-MM-dd_hh-mm
- $LogFile = ".\SPServerDiskSpaceDetailsPatch-$LogTime.rtf"
-
- # Add SharePoint PowerShell Snapin
- if ( (Get-PSSnapin -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null ) {
- Add-PSSnapin Microsoft.SharePoint.Powershell
- }
-
- $scriptBase = split-path $SCRIPT:MyInvocation.MyCommand.Path -parent
- Set-Location $scriptBase
-
- $OutputFile = $scriptBase + "\" + "SPServerSpaceDetails.csv"
-
- #Removing old output files
- if (test-path $OutputFile)
- {
- remove-item $OutputFile
- }
-
- #Removing old log files
- $FindRTFFile = Get-ChildItem $scriptBase\*.* -include *.rtf
- if($FindRTFFile)
- {
- foreach($file in $FindRTFFile)
- {
- remove-item $file
- }
- }
start-transcript $logfile
- #Server list details
-
- $serverList = $scriptBase + "\" + "ServerList.txt";
-
- $output = $OutputFile
-
- "Computer Name" + "," + "Drive" + "," + "Size in (GB)" + "," + "Free Space in (GB)" + "," + "Critical (*)" | Out-File -Encoding Default -FilePath $Output;
-
- foreach($computer in get-content $serverList)
- {
-
- $drives = Get-WmiObject -ComputerName $computer Win32_LogicalDisk | Where-Object {$_.DriveType -eq 3}
-
- foreach($drive in $drives)
-
- {
-
- $id = $drive.DeviceID
-
- $size = [math]::round($drive.Size / 1073741824, 2)
-
- $free = [math]::round($drive.FreeSpace / 1073741824, 2)
-
- $pct = [math]::round($free / $size, 2) * 100
-
- if ($pct -lt 30)
-
- {
- $pct = $pct.ToString() + "% *** "
-
- $emailFrom = "[email protected]"
- # Use commas for multiple addresses
- $emailTo = "[email protected]"
- $subject = "URGENT!!!! " + $drive.DeviceID + " drive is running out of space in SharePoint server"
- $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."
- $smtpServer = "SMTP Server address"
- $smtp = new-object Net.Mail.SmtpClient($smtpServer)
- $smtp.Send($emailFrom, $emailTo, $subject, $body)
-
- }
-
- else { $pct = $pct.ToString() + " %" }
-
- $computer + "," + $id + "," + $size + "," + $free + "," + $pct | Out-File -Encoding Default -Append -FilePath $Output;
-
- $pct = 0
-
- }
-
- }
-
- $EmailFrom = "[email protected]"
- $EmailTo = "[email protected]"
- $EmailSubject = "Available free space in SharePoint servers"
- $emailbody = "Hi SharePoint Admin Team,
Kindly find the attached free space details for the SharePoint servers.
Thanks,
SharePoint DiskSpace Monitoring Script."
- #enter the SMTP server name or IP
-
- $SMTPServer = "SMTP server address"
- #$SMTPAuthUsername = "username"
- #$SMTPAuthPassword = "password"
-
- $emailattachment = $OutputFile
-
- function send_email {
- $mailmessage = New-Object system.net.mail.mailmessage
- $mailmessage.from = ($emailfrom)
- $mailmessage.To.add($emailto)
- $mailmessage.Subject = $emailsubject
- $mailmessage.Body = $emailbody
-
- $attachment = New-Object System.Net.Mail.Attachment($emailattachment, 'text/plain')
- $mailmessage.Attachments.Add($attachment)
- #$mailmessage.IsBodyHTML = $true
- $SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 25)
- $SMTPClient.Credentials = New-Object System.Net.NetworkCredential("$SMTPAuthUsername", "$SMTPAuthPassword")
- $SMTPClient.Send($mailmessage)
- }
-
- send_email
-
- stop-transcript