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

Posted on 2009-02-11
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

    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 = ""        'Enter the from address the email will appear from
    Const emailTo = ""          '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 ("") = 2
            .Configuration.Fields.Item ("") = ExchangeServer
            .Configuration.Fields.Item ("") = 25
        end with
        Set objMessage = Nothing
    End Sub

    Open in new window


    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    Join & Write a Comment

    I met Paul Devereux (@pdevereux) today when I responded to his tweet asking “Anybody know how to automate adding files from disk to a folder in #outlook  ?”.  I replied back and told Paul that using automation, in this case scripting, to add files t…
    It is a general practice to get rid of old user profiles on a computer  in a LAN environment. As I have been working with a company in a LAN environment where users move from one place to some other place at times. This will make many user profil…
    Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
    The viewer will learn how to count occurrences of each item in an array.

    746 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

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now