Solved

Script to Check Hard Drive Space and Email Output

Posted on 2014-09-19
5
547 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 33

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 33

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

While rebooting windows server 2003 server , it's showing "active directory rebuilding indices please wait" at startup. It took a little while for this process to complete and once we logged on not all the services were started so another reboot is …
A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

820 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