Learn how to a build a cloud-first strategyRegister Now


Script to check free space on windows server and send email alert

Posted on 2009-02-11
Medium Priority
Last Modified: 2012-05-06
Does someone have a simple script which monitors my windows server and if the space falls below a certain limit, then sends out an email alert. I searched on net, but nothing is working correctly and some with some errors also. Am not good in scripting.


Question by:chthomas
1 Comment
LVL 14

Accepted Solution

rejoinder earned 300 total points
ID: 23686504
All the variables you need to set are stored at the top of the script.  Edit as required.

This script assumes you have an Exchange email server available and that you have the appropriate permissions to the servers.
Const WarningLow = .11                        'This is the lower percentage of free space for a warning; 11% = .11
Const WarningHigh = .35                       'This is the upper percentage of free space for a warning; 35% = .35
Const AlertHigh = .1                          'This is the upper percentage of free space for an alert; 10% = .1
Const emailFrom = "someone@domain.com"        'Enter the from address the email will appear from
Const emailTo = "someone@domain.com"          'Enter the to address which the email will be sent to
Const ExchangeServer = "EXCHANGE-SERVER-NAME" 'Enter your Exchange server name here
Const WaitTimeInMinutes = 15                  'Wait time between polls measured in minutes
Dim strMessage
Dim arrServerList
arrServerList = array("SERVER1","SERVER2")    'Enter the list of servers to monitor
Do until i = 2
    'Clear the message variable
    strMessage = ""
    'Poll the array of servers
    'Email if there is a message
    if strMessage <> "" then
    end if
    'Wait for set number of minutes then loop
    'Uncomment next line to test a few loops and quit
    'i = i + 1
Sub PollServers(arrServers)
    on error resume next
    for each Server in arrServers
        set objSvc = GetObject("winmgmts:{impersonationLevel=impersonate}//" & Server & "/root/cimv2")
        set objRet = objSvc.InstancesOf("win32_LogicalDisk")
        for each item in objRet
            if item.DriveType = 3 then
                if item.FreeSpace/item.size <= WarningHigh and item.FreeSpace/item.size >= WarningLow then
                    strMessage = strMessage & UCase(Server) & ": Warning, drive '" & item.caption & "' is running low on HDD space.  There are " & FormatNumber((item.FreeSpace/1024000),0) & " MB free." & vbCRLF
                end if
                if item.FreeSpace/item.size <= AlertHigh then
                    strMessage = strMessage & UCase(Server) & ": Alert, drive '" & item.caption & "' is now low on HDD space!  There are " & FormatNumber((item.FreeSpace/1024000),0) & " MB free." & vbCRLF
                end if
            end if
    set objSvc = Nothing
    set objRet = Nothing
End Sub
Sub EmailAlert(Message)
    on error resume next
    Set objMessage = CreateObject("CDO.Message")
    with objMessage
        .From = emailFrom
        .To = emailTo
        .Subject = "HDD Status Update"
        .TextBody = Message
        .Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
        .Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = ExchangeServer
        .Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
    end with
    Set objMessage = Nothing
End Sub

Open in new window


Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This script will sweep a range of IP addresses (class c only, and report to a log the version of office installed. What it does: 1.)      Creates log file in the directory the script is run from (if it doesn't already exist) 2.)      Sweep…
This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses
Course of the Month20 days, 18 hours left to enroll

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question