Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sending an e-mail based on a script I have 2

Status
Not open for further replies.

DrSeussFreak

Programmer
Feb 16, 2007
149
US
I am using this code to check for any drives that are more than 89.9999% free. What I need is to add an e-mail function to my script so that when (pFree > "89.9999999") pulls a value, it lets me know.

Any ideas? Thanks.

Code:
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set colDrives = objFSO.Drives

For Each objDrive in colDrives

DIM dLetter
DIM fSpace
DIM tSize
DIM pFree

    dLetter = objDrive.DriveLetter
    fSpace = objDrive.FreeSpace
    tSize = objDrive.TotalSize
    pFree = ((objDrive.FreeSpace / objDrive.TotalSize) * 100)

if pFree > "89.9999999" then
    Wscript.Echo dLetter & " is low on space, and is currently at " & pFree & " space free."
else
    Wscript.Echo dLetter & " is currently at " & pFree & " and so is OK!"
End if
Next
 
Also, just as an FYI, once I get the e-mail working, the (Wscript.Echo) will be removed, since this will become an automated task.

Thanks again.
 
Do a search for CDO and you will find examples for emailing.

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
The following subroutine will send emails via an SMTP server

You can include an attachment by adding this line: objEmail.AddAttachment "C:\file.ext"

Code:
Sub SendEmail(strTo, strFrom, strSubject, strMsg)
	schema = "[URL unfurl="true"]http://schemas.microsoft.com/cdo/configuration/"[/URL]
	SmtpServer	= "" '<<< Put your SMTP server address in here

	Set objEmail = CreateObject("CDO.Message")
	objEmail.From = strFrom
	objEmail.To = strTo
	objEmail.Subject = strSubject
	objEmail.Textbody = strMsg
	objEmail.Textbody = objEmail.Textbody & vbcrlf

	objEmail.Configuration.Fields.Item (schema & "sendusing") = 2
	objEmail.Configuration.Fields.Item (schema & "smtpserver") = SmtpServer
	objEmail.Configuration.Fields.Item (schema & "smtpserverport") = 25
	objEmail.Configuration.Fields.Update
	objEmail.Send
End Sub
 
sorry, take out this line: objEmail.Textbody = objEmail.Textbody & vbcrlf

it was left over from something I'd done in one of my scripts.
 
I get

C:\>DriveSpaceChecker.vbs
Microsoft (R) Windows Script Host Version 5.6
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.

C:\DriveSpaceChecker.vbs(20, 4) Microsoft VBScript compilation error: Syntax error

From

Code:
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set colDrives = objFSO.Drives

For Each objDrive in colDrives

DIM dLetter
DIM fSpace
DIM tSize
DIM pFree

    dLetter = objDrive.DriveLetter
    fSpace = objDrive.FreeSpace
    tSize = objDrive.TotalSize
    pFree = ((objDrive.FreeSpace / objDrive.TotalSize) * 100)

if pFree > "89.9999999" then
   
   'Wscript.Echo dLetter & " is low on space, and is currently at " & pFree & " space free."
   
   Sub SendEmail(strTo, strFrom, strSubject, strMsg)
    schema = "[URL unfurl="true"]http://schemas.microsoft.com/cdo/configuration/"[/URL]
    SmtpServer    = "MY SMTP HERE" '<<< Put your SMTP server address in here

    Set objEmail = CreateObject("CDO.Message")
    objEmail.From = strFrom
    objEmail.To = strTo
    objEmail.Subject = strSubject
    objEmail.Textbody = strMsg
    
    strFrom = "UIMCMRSDB02"
    strTo = "mfederma@uic.edu"
    strSubject = "Mars is low on space"
    strMsg = dLetter & " is low on space, and is currently at " & pFree & " space free."   

    objEmail.Configuration.Fields.Item (schema & "sendusing") = 2
    objEmail.Configuration.Fields.Item (schema & "smtpserver") = SmtpServer
    objEmail.Configuration.Fields.Item (schema & "smtpserverport") = 25
    objEmail.Configuration.Fields.Update
    objEmail.Send
End Sub
   
else
    Wscript.Echo dLetter & " is currently at " & pFree & " and so is OK!"
End if
Next
 
without the mail function, it returns

C:\>DriveSpaceChecker.vbs
Microsoft (R) Windows Script Host Version 5.6
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.

C is currently at 16.2510423827286 and so is OK!
D is currently at 81.2799071528063 and so is OK!
K is currently at 60.0603042402835 and so is OK!
L is currently at 27.3959296712331 and so is OK!
M is currently at 37.7613895085121 and so is OK!
N is low on space, and is currently at 93.7834371700745 space free.
O is currently at 82.5033746327606 and so is OK!
 
The sub should go outside you For...Next loop

You call it with something like:
SendEmail "someone@someplace.com", "you@someplace.com", "Script Warning", "Body Text"

That is what you add in your For...Next area.

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
Using this logic, how do I get the drive that matches my criteria to be included since it is in a for loop?

Code:
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set colDrives = objFSO.Drives

For Each objDrive in colDrives

Dim dLetter
Dim fSpace
Dim tSize
Dim pFree
Dim vName

    dLetter = objDrive.DriveLetter
    fSpace = objDrive.FreeSpace
    tSize = objDrive.TotalSize
    pFree = ((objDrive.FreeSpace / objDrive.TotalSize) * 100)
    vName = objDrive.VolumeName

if pFree > "89.9999999" then
   
   Wscript.Echo  dLetter & " (" & vName & ")" & " is low on space, and is currently at " & pFree & " space free."
   
else
    Wscript.Echo dLetter & " (" & vName & ")" & " is currently at " & pFree & " and so is OK!"
End if
Next

Sub SendEmail("Me", "Sender", "Mars is low on space", "")
    schema = "[URL unfurl="true"]http://schemas.microsoft.com/cdo/configuration/"[/URL]
    SmtpServer = "Server" '<<< Put your SMTP server address in here

    Set objEmail = CreateObject("CDO.Message")
    
    objEmail.Configuration.Fields.Item (schema & "sendusing") = 2
    objEmail.Configuration.Fields.Item (schema & "smtpserver") = SmtpServer
    objEmail.Configuration.Fields.Item (schema & "smtpserverport") = 25
    objEmail.Configuration.Fields.Update
    objEmail.Send
End Sub
 
No, should look something like this

Code:
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set colDrives = objFSO.Drives

Dim dLetter
Dim fSpace
Dim tSize
Dim pFree
Dim vName

For Each objDrive in colDrives
	dLetter = objDrive.DriveLetter
	fSpace = objDrive.FreeSpace
	tSize = objDrive.TotalSize
	pFree = ((objDrive.FreeSpace / objDrive.TotalSize) * 100)
	vName = objDrive.VolumeName
	
	If pFree > "89.9999999" Then
		SendEmail "you@someplace.com", "script", "Low Free Space Warning", _
				  dLetter & " (" & vName & ")" & " is low on space, and is currently at " & pFree & " space free."
' 		Wscript.Echo  dLetter & " (" & vName & ")" & " is low on space, and is currently at " & pFree & " space free."
	Else
		
' 		Wscript.Echo dLetter & " (" & vName & ")" & " is currently at " & pFree & " and so is OK!"
	End If
Next

Sub SendEmail(strTo, strFrom, strSubject, strMsg)
    schema = "[URL unfurl="true"]http://schemas.microsoft.com/cdo/configuration/"[/URL]
    SmtpServer    = "" '<<< Put your SMTP server address in here

    Set objEmail = CreateObject("CDO.Message")
    objEmail.From = strFrom
    objEmail.To = strTo
    objEmail.Subject = strSubject
    objEmail.Textbody = strMsg
    objEmail.Textbody = objEmail.Textbody & VbCrLf

    objEmail.Configuration.Fields.Item (schema & "sendusing") = 2
    objEmail.Configuration.Fields.Item (schema & "smtpserver") = SmtpServer
    objEmail.Configuration.Fields.Item (schema & "smtpserverport") = 25
    objEmail.Configuration.Fields.Update
    objEmail.Send
End Sub

You should read on how to use Sub/Functions!

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
Thanks guys, it worked perfectly. Much obliged folks! Stars were given!
 
You should read on how to use Sub/Functions!

I agree. I am a oracle database administrator, I am just trying to get some of my unix scripts to run on windows boxes, I have about 8 more to write :p
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top