• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1809
  • Last Modified:

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

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.

Regards,

Charley
0
chthomas
Asked:
chthomas
1 Solution
 
rejoinderCommented:
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
    PollServers(arrServerList)
    
    'Email if there is a message
    if strMessage <> "" then
        EmailAlert(strMessage)
    end if
    
    'Wait for set number of minutes then loop
    WScript.Sleep(WaitTimeInMinutes*60000)
    
    'Uncomment next line to test a few loops and quit
    'i = i + 1
Loop
 
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
        next
    next
    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
        .Configuration.Fields.Update
        .Send
    end with
    Set objMessage = Nothing
End Sub

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now