Link to home
Create AccountLog in
Avatar of InfrastrucutureServices
InfrastrucutureServices

asked on

Powershell script to alert low space on Exchange mount points

Hi,

I have a script which currently reports mount point disk space on our Exchange servers, I would like to refine it so it only reports low disk space.

This is out current script:

function get-mountpoints {

param( $server )
$TotalGB = @{Name="Capacity(GB)";expression={[math]::round(($_.Capacity/ 1073741824),2)}}
$FreeGB = @{Name="FreeSpace(GB)";expression={[math]::round(($_.FreeSpace / 1073741824),2)}}
$FreePerc = @{Name="Free(%)";expression={[math]::round(((($_.FreeSpace / 1073741824)/($_.Capacity / 1073741824)) * 100),0)}}
$volumes = Get-WmiObject -ComputerName $server -Class win32_volume | Where-object {$_.DriveLetter -eq $null}
$volumes | select systemname, label, $TotalGB, $FreeGB, $FreePerc | ConvertTo-Html | Out-String  
}
$messageParameters = @{
Subject = "Exchange DiskSpace Report " + (Get-Date -Format g)
body = (Get-Content c:\batch\diskspace2\servers.txt | ForEach-Object {
get-mountpoints $_
} ) | out-string
From = "exchangereports@company.com" 
To = "myself@company.com"
SmtpServer = "smtp.com" 
BodyAsHtml = $true
}
Send-MailMessage @messageParameters

Open in new window


The following script does what I want but only for fixed drives, if possible I would like it updated to use mount points:

#########################################################
#
# Disk space monitoring and reporting script
#
#########################################################
 

$users = "myself@company.com" # List of users to email your report to (separate by comma)
$fromemail = "fromaddress@yourdomain.com"
$server = "smtp.com" #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 = 20
 

#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 = @"
<font color=""black"" face=""Arial, Verdana"" size=""3"">
<u><b>Disk Space Storage Report</b></u>
<br>This report was generated because the drive(s) listed below have less than $thresholdspace % free space. Drives above this threshold will not be listed.
<br>
<style type=""text/css"">body{font: .8em ""Lucida Grande"", Tahoma, Arial, Helvetica, sans-serif;}
ol{margin:0;padding: 0 1.5em;}
table{color:#FFF;background:#C00;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;}
</style>
<body BGCOLOR=""white"">
$tableFragment
</body>
"@ 
 

# Set up a regex search and match to look for any <td> 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)<td>'
 

# 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

Open in new window


Thanks.
ASKER CERTIFIED SOLUTION
Avatar of SubSun
SubSun
Flag of India image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of InfrastrucutureServices
InfrastrucutureServices

ASKER

Thanks, that does exactly what I wanted, I have requested another feature in a seperate question, which is to add the available whitespace as an additional column for each DB in that report

https://www.experts-exchange.com/questions/28179169/Add-available-WhiteSpace-report-to-diskspace-report-for-Exchange-2007-mountpoints.html