Powershell script to monitor mount points in Windows and e-mail an alert

Simple script inspired by this blog post.

This script will read a servers.txt file located in the same directory as the script, check each server listed (one per line, use the servers FQDN) for mount points, see how much free space is available, compare it to a minimum amount you set and then e-mail an alert if below the minimum amount.

Should work on Windows Server 2008 and newer. Might work on Server 2003 but I haven’t tested it.

# Script to monitor storage on disk mount points. Orignally created for Exchange.
# Created by: Eric Schewe
# Created on: 2016-01-06
# Original Source: http://www.powershellneedfulthings.com/?p=36
#
# Create a server.txt file in the same directory as the script with a list of servers in it
#

# How low in GB do you want to let free space get before notification?
# You can go up to two decimal spaces if you want
$lowSpace = 10.00

# E-mail stuff
# Multiple e-mail addresses should be in this format "<[email protected]>, <[email protected]somewhere.com>"
$to = ""
$from = "[email protected]"
$smtpServer = "smtp.localdomain"

# Injest Server list, one server FQDN per line
# You may need to have the full path to the servers.txt here if you're running this as a scheduled task
$servers = (Get-Content "servers.txt")

# Check each server
foreach ($server in $servers)
{

    # First check if the server is up
    if ((Test-Connection -quiet $server) -eq $true) {

        # Get all the volumes on the server with out a drive letter
        $volumes = Get-WmiObject -computer $server win32_volume | Where-object {$_.DriveLetter -eq $null}

        # Check each volume found
        foreach ($volume in $volumes) {

            # Skip the System Reserved volume. It will always be low
            if ($volume.Label -ne "System Reserved") {

                # Do some math to convert bytes into GB, rounded and 2 decimal places
                if (([math]::round(($volume.FreeSpace / 1073741824),2)) -le $lowSpace) {

                    # Send an e-mail notification including low disk space setting, server fqdn and volume name
                    $subject = "Low disk space warning (below $($lowSpace)GB) - $($server) - $($volume.Label)"
                    $smtp = new-object Net.Mail.SmtpClient($smtpServer)
                    $smtp.Send($from, $to, $subject, "")

                }

            }

        }

    }

}

 

7 thoughts on “Powershell script to monitor mount points in Windows and e-mail an alert

    • I would suspect because either the ‘To’ address isn’t set correctly, your SMTP server isn’t set correctly or the server that runs the script doesn’t have access to relay e-mail via your configured SMTP server.

  1. hello, how can I make it work with %. say for example, I like to get warning if it below 10%. thank you.

    Golam Kabir

  2. Hi Eric,

    How to use this if one likes to make the lowspace on %. For example, i like to get noticed if the volume space is lower than 10%. Thank you

    • You could modify the script to do some math for you (current size divided by max size) to get the percentage and then change the ‘if’ statement to look at that percentage instead of the hard limit I have.

      We prefer absolutes instead of percentages so that’s why I wrote the script this way.

  3. Eric,

     

    in my environment some volumes like cluster resource are so small that an absolute value make no sense. therefore, i’ve been thinking about going %. I’ve tried the following

     

    # Do some math to convert bytes into GB, rounded and 2 decimal places
    if (([math]::round(($volume.FreeSpace / volume.size) * 100,2)) -le $lowSpace).

    this give me no email. could you please guide me . thank you.

    • The first thing to find out is whether or not the issue is your script is or isn’t triggering properly. Set the minimum number to something that will trigger no matter what. If you don’t get an e-mail then the issue is the mail settings. Did you configure your SMTP properly? and is it allowing you to relay from the server you are running the script on?

      If SMTP isn’t the issue then it’s the trigger condition. I can’t help you much there since you’re changing the script. I’d recommend hitting up stackexchange.com

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.