Link to home
Start Free TrialLog in
Avatar of ozzalot
ozzalotFlag for United States of America

asked on

Script to Check Hard Drive Space and Email Output

Hello!!

I'm not that good with scripting and i've been researching a script to check the hard drives of my servers and email a report.  I found a script here at Experts Exchange but I can't get it to work.  I'm sure I have to modify it for my enviroment but i'm not sure what I have to modify. Can someone help me out please?  I'm trying this on Windows 7 and servers are either Server 2008 or 2008 R2.

I have it set to 60GB as i'm trying to test out my local computer.  I've changed the To and From email address and the IP address of the mail server.  Other than that the script is tact.  I'll attach the error I'm getting.  It has to do with the last line of code oEmail.Send    User generated image
Option Explicit
'===========================================================================
'  Scheduled Task - Visual Basic ActiveX Script
'===========================================================================
'  Desc    : VBScript file that sends short-on-disk warnings
'===========================================================================

'--  Check that server has minimum disk space (in MBs)
Call CheckDrives(60000)




'===========================================================================
'  Name    : CheckDrives(intMinMBSize)
'  Desc    : Check Disk Space on Fixed Drives and report on short space
'===========================================================================
Function CheckDrives(intMinMBSize)
   DIM oFileSystem, oDrive, oDriveCollection
   DIM blnShortSpace, strErrorMsg, intMBSize

   blnShortSpace = False
   strErrorMsg = ""

   '-- Check each Fixed Drive on this Machine
   Set oFileSystem = CreateObject("Scripting.FileSystemObject")
   Set oDriveCollection = oFileSystem.Drives
   For Each oDrive In oDriveCollection
      '-- Check Fixed Drives Only
      If oDrive.DriveType = 2 Then
         '-- Get Disk Size as MBs (Bytes / 1024 = KBytes, KBytes / 1024 = MBytes)
         intMBSize = oDrive.FreeSpace / 1048576

         '-- Rise a Flag when less than Minimum
         If intMBSize < intMinMBSize Then
            blnShortSpace = True
            strErrorMsg = strErrorMsg & "Drive " & oDrive.DriveLetter & ": - Free Space Left is " & FormatNumber(intMBSize, 2) & " MB" & vbCrLf
         End If
      End If
   Next
   Set oDriveCollection = Nothing
   Set oFileSystem = Nothing

   '-- Send Email if there is short space left on this machine
   If blnShortSpace = True Then Call SendAlertEmail("Running Low on Disk Space", _
       strErrorMsg)
End Function


'===========================================================================
'  Name    : GetMachineName()
'  Desc    : Determine Computer's Machine Name
'===========================================================================
Function GetMachineName()
    On Error Resume Next

    '-- Try Network Object
    DIM WshNetwork

    Set WshNetwork = CreateObject("WScript.Network")
    GetMachineName = WshNetwork.ComputerName
    Set WshNetwork = Nothing

    If IsNull(GetMachineName) Or GetMachineName = "" Then
        '-- Try Shell Object
        DIM wshShell

        Set wshShell = CreateObject("WSCript.Shell")
        GetMachineName = wshShell.RegRead _
            ("HKLM\SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName\ComputerName")
        Set wshShell = Nothing

        If IsNull(GetMachineName) Or GetMachineName = "" Then
            '-- Try Windows Mgmt Implementation
            DIM objWMIService, OSItems, objItem
       
            Set objWMIService = GetObject _
               ("winmgmts:" & "{impersonationLevel=impersonate}!\\.\root\cimv2")
            Set OSItems = objWMIService.ExecQuery("Select * from Win32_OperatingSystem", , 48)
            For Each objItem In OSItems
            GetMachineName = objItem.CSName
            Next
        End If
    End If
    Err.Clear

    If IsNull(GetMachineName) Or GetMachineName = "" Then GetMachineName = "LOCALHOST"
End Function


'===========================================================================
'  Name    : SendAlertEmail(Subject Line, Email Body)
'  Desc    : Send Error/Alert Email
'===========================================================================
Function SendAlertEmail(strSubject, strErrorText)
'-- Send Email
    Set cdoConfig = CreateObject("CDO.Configuration")  
   
    Const cdoSendUsingPickup = 1
    Const cdoSendUsingPort = 2
    Const cdoAnonymous = 0
    Const cdoBasic = 1
    Const cdoNTLM = 2
    Const cdoSendUsingMethod = "http://schemas.microsoft.com/cdo/configuration/sendusing"
    Const cdoSMTPServer = "http://schemas.microsoft.com/cdo/configuration/smtpserver"

    With cdoConfig.Fields  
        .Item(cdoSendUsingMethod) = cdoSendUsingPort
        .Item(cdoSMTPServer) = "xxx.xxx.xxx.xxx"
        .Update  
    End With
    Set oEmail = CreateObject("CDO.Message")
    DIM oEmail, strMachineName, cdoConfig
    oEmail.To   = "myemail@domain.com"
    oEmail.From = "HDTest@mydomain.com"
    oEmail.Subject = strMachineName & " : " & strSubject & " @ " & Now
    oEmail.TextBody = strErrorText
    oEmail.Send()
    Set oEmail = Nothing
End Function
Avatar of it_saige
it_saige
Flag of United States of America image

In this line

     .Item(cdoSMTPServer) = "xxx.xxx.xxx.xxx"

Open in new window


Did you specify an SMTP server?

-saige-
Avatar of ozzalot

ASKER

Yes i did. I put the up IP address inside the quotes.
ASKER CERTIFIED SOLUTION
Avatar of it_saige
it_saige
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ozzalot

ASKER

Saige,

Your a genius!! it works.  I'm running on servers that have multiple drives. How can i add a newline character?  As of right now it displays information of all the drives in one single line.
Avatar of ozzalot

ASKER

Nevermind I figured it out the newline problem.  Thanks alot Saige!!  I've been scratching my head on this one. I need to freshen up my programming. Haven't done any since the college days.