Solved

Script to Check Hard Drive Space and Email Output

Posted on 2014-09-19
5
443 Views
Last Modified: 2014-09-19
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
Comment
Question by:ozzalot
  • 3
  • 2
5 Comments
 
LVL 32

Expert Comment

by:it_saige
ID: 40333366
In this line

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

Open in new window


Did you specify an SMTP server?

-saige-
0
 

Author Comment

by:ozzalot
ID: 40333379
Yes i did. I put the up IP address inside the quotes.
0
 
LVL 32

Accepted Solution

by:
it_saige earned 500 total points
ID: 40333436
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
 

Author Comment

by:ozzalot
ID: 40333486
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
 

Author Closing Comment

by:ozzalot
ID: 40333507
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles from a Windows Server 2008 domain controller to a Windows Server 2012 domain controlle…
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles to another domain controller. Log onto the new domain controller with a user account t…

707 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

20 Experts available now in Live!

Get 1:1 Help Now