Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 845
  • Last Modified:

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    2014-09-19-141838.jpg
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
0
ozzalot
Asked:
ozzalot
  • 3
  • 2
1 Solution
 
it_saigeDeveloperCommented:
In this line

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

Open in new window


Did you specify an SMTP server?

-saige-
0
 
ozzalotAuthor Commented:
Yes i did. I put the up IP address inside the quotes.
0
 
it_saigeDeveloperCommented:
Try this one:

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"
	Const cdoSMTPServerPort = "http://schemas.microsoft.com/cdo/configuration/smtpserverport"

	With cdoConfig.Fields  
		.Item(cdoSendUsingMethod) = cdoSendUsingPort
		.Item(cdoSMTPServer) = "xxx.xxx.xxx.xxx"
		.Item(cdoSMTPServerPort) = 25
		.Update  
	End With
	Set oEmail = CreateObject("CDO.Message")
	DIM oEmail, strMachineName, cdoConfig
	oEmail.To   = "myemail@domain.com"
	oEmail.From = "HDTest@domain.com"
	oEmail.Subject = strMachineName & " : " & strSubject & " @ " & Now
	oEmail.TextBody = strErrorText
	oEmail.Configuration = cdoConfig
	oEmail.Send()
	Set oEmail = Nothing
End Function

Open in new window


I added this line:

	Const cdoSMTPServerPort = "http://schemas.microsoft.com/cdo/configuration/smtpserverport"

Open in new window


Which changed this:

	With cdoConfig.Fields  
		.Item(cdoSendUsingMethod) = cdoSendUsingPort
		.Item(cdoSMTPServer) = "xxx.xxx.xxx.xxx"
		.Item(cdoSMTPServerPort) = 25
		.Update  
	End With

Open in new window


And attached the configuration object to the mail object:

	oEmail.Configuration = cdoConfig

Open in new window


HTH,

-saige-
0
 
ozzalotAuthor Commented:
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.
0
 
ozzalotAuthor Commented:
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.
0

Featured Post

Technology Partners: 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!

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