Created
August 6, 2016 02:16
-
-
Save Dan1el42/e2d89b0921373eef299c74d36037915e to your computer and use it in GitHub Desktop.
Post reference: https://powershell.org/forums/topic/powershell-script-to-send-an-email-notification-with-servers-with-disk-space/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Script Disk Space | |
$users = “xxxxxxxx” # List of users to email your report to (separate by comma) | |
$fromemail = “xxxxxxxxx” | |
$server = “xxxxxxx” #enter your own SMTP server DNS name / IP address here | |
$list = "C:\Powershell Script\list.txt" #This accepts the argument you add to your scheduled task for the list of servers. i.e. list.txt | |
$computers = Get-Content -Path $list #grab the names of the servers/computers to check from the list.txt file. | |
$mydate = Get-Date | |
# Set free disk space threshold below in percent (default at 10%) | |
[decimal]$thresholdspace = 10 | |
#assemble together all of the free disk space data from the list of servers and only include it if the percentage free is below the threshold we set above. | |
$SelectPropertyList = @( | |
'__SERVER' | |
'DriveType' | |
'VolumeName' | |
'Name' | |
@{Name = 'Size (Gb)'; Expression = {“{0:n2}” -f ($_.size/1gb)}} | |
@{Name = 'FreeSpace (Gb)'; Expression = {“{0:n2}” -f ($_.freespace/1gb)}} | |
@{Name = 'PercentFree'; Expression = {“{0:n2}” -f ($_.freespace/$_.size*100)}} | |
) | |
$tableFragment = | |
Get-WMIObject -ComputerName $computers Win32_LogicalDisk | | |
Select-Object -Property $SelectPropertyList | | |
Where-Object { $_.DriveType -eq 3 -and [decimal]$_.PercentFree -lt [decimal]$thresholdspace } | | |
Sort-Object -Property PercentFree | | |
ConvertTo-HTML -fragment | |
# assemble the HTML for our body of the email report. | |
$HTMLmessage = @” | |
Disk Space Storage Report | |
This report was generated because the drive(s) listed below have less than $thresholdspace % free space. Drives above this threshold will not be listed. | |
body{font: .8em “”Lucida Grande””, Tahoma, Arial, Helvetica, sans-serif;} | |
ol{margin:0;padding: 0 1.5em;} | |
table{color:#000000;background:#FFFFFF;border-collapse:collapse;width:647px;border:5px solid #900;} | |
thead{} | |
thead th{padding:1em 1em .5em;border-bottom:1px dotted #FFF;font-size:120%;text-align:left;} | |
thead tr{} | |
td{padding:.5em 1em;} | |
tfoot{} | |
tfoot td{padding-bottom:1.5em;} | |
tfoot tr{} | |
#middle{background-color:#900;} | |
$tableFragment | |
Generated on $mydate | |
“@ | |
# Set up a regex search and match to look for any tags in our body. These would only be present if the script above found disks below the threshold of free space. | |
# We use this regex matching method to determine whether or not we should send the email and report. | |
$regexsubject = $HTMLmessage | |
$regex = [regex] '(?im)' | |
# if there was any row at all, send the email | |
if ($regex.IsMatch($regexsubject)) | |
{ | |
$date = (Get-Date).ToString('yyyy/MM/dd') | |
Send-MailMessage -From $fromemail -To $users -Subject “SQLServerDiskSpace Report -$date” -BodyAsHTML -body $HTMLmessage -Priority High -SmtpServer $server | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment