We help IT Professionals succeed at work.

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

chthomas
chthomas asked
on
Medium Priority
1,818 Views
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.

Regards,

Charley
Comment
Watch Question

Manager, IT
CERTIFIED EXPERT
Top Expert 2008
Commented:
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

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.