######################################################### # # Disk space monitoring and reporting script # ######################################################### $users = "santa-support@datapipe.com" # List of users to email your report to (separate by comma) $fromemail = "dcook@datapipe.com" $server = "mail5.datapipe.net" #enter your own SMTP server DNS name / IP address here $list = $args[0] #This accepts the argument you add to your scheduled task for the list of servers. i.e. list.txt $computers = get-content $list #grab the names of the servers/computers to check from the list.txt file. # 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. $tableFragment= Get-WMIObject -ComputerName $computers Win32_LogicalDisk ` | select __SERVER, DriveType, VolumeName, Name, @{n='Size (Gb)' ;e={"{0:n2}" -f ($_.size/1gb)}},@{n='FreeSpace (Gb)';e={"{0:n2}" -f ($_.freespace/1gb)}}, @{n='PercentFree';e={"{0:n2}" -f ($_.freespace/$_.size*100)}} ` | Where-Object {$_.DriveType -eq 3 -and [decimal]$_.PercentFree -lt [decimal]$thresholdspace} ` | 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.
$tableFragment "@ # 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)) { send-mailmessage -from $fromemail -to $users -subject "Disk Space Monitoring Report" -BodyAsHTML -body $HTMLmessage -priority High -smtpServer $server } # End of Script