• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 533
  • Last Modified:

VB Script: Inserting %Userprofile% and sending email with the log file text in the body.

Hi, I'm new to the VB world, but I found this great script written by Andrew Taylor.  It works great, but I need to make two additions.  
First, In line 48 (set constants), instead of writing c:\documents and settings\userid, I would like to insert (%userprofile%) but it's giving me a path not found error.  
Second, instead of attaching the log file in the email, is there a way to get the text from the log file straight into the email body?
Thank you very much for your time.
Option Explicit
 
' ***************************************************************
' * Email NT Backup log file
' * © Andrew Taylor 2008 www.andrewtaylor.me.uk
' * Free to use providing this acknowledgement remains in place
' ***************************************************************
 
' Dim variables
Dim oFSO, oWshNetwork, oCDO, oFolder, oFiles, oFile
Dim strFileName, strComputerName
 
' Create Objects
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oWshNetwork = WScript.CreateObject("WScript.Network")
 
 
' Set constants
Const BACKUP_LOG_PATH = "(%USERPROFILE%)\Local Settings\Application Data\Microsoft\Windows NT\NTBackup\data"
Const EMAIL_RECIPIENT = "emailaddress@company.com"
 
' Set variable defaults
strFileName = ""
strComputerName = oWshNetwork.ComputerName
 
' Start script
Set oFolder = oFSO.GetFolder(BACKUP_LOG_PATH)
 
Set oFiles = oFolder.Files
 
For Each oFile In oFiles
	If oFSO.GetExtensionName(oFile.Name) = "log" Then
		If DateValue(oFile.DateLastModified) = Date() Then
			strFileName = oFile.Name
			Exit For
		End If
	End If
Next
 
If strFileName <> "" Then
 
	EmailLog(strFileName)
 
Else
 
	EmailError
 
End If
 
Sub EmailLog(logFilePath)
 
	Set oCDO = CreateObject("CDO.Message")
 
	oCDO.Subject = "Backup Log - " & strComputerName
	oCDO.From = strComputerName & "@company.com"
	oCDO.To = EMAIL_RECIPIENT
 
	oCDO.TextBody = "Backup log attached for " & strComputerName
 
	oCDO.AddAttachment BACKUP_LOG_PATH & "\" & logFilePath
 
	oCDO.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
 
	oCDO.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") ="smtpserver"
 
	oCDO.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25 
 
	oCDO.Configuration.Fields.Update
 
	oCDO.Send
 
	Set oCDO = Nothing
 
 
End Sub
 
Sub EmailError
 
	Set oCDO = CreateObject("CDO.Message")
 
	oCDO.Subject = "Backup Log Missing - " & strComputerName
	oCDO.From = strComputerName & "@company.com"
	oCDO.To = EMAIL_RECIPIENT
 
	oCDO.TextBody = "No backup log found on " & strComputerName & " for today"
 
	oCDO.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
 
	oCDO.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") ="smtpserver"
 
	oCDO.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25 
 
	oCDO.Configuration.Fields.Update
 
	oCDO.Send
 
	Set oCDO = Nothing
 
 
End Sub
 
Set oFSO = Nothing
Set oWshNetwork = Nothing

Open in new window

0
esi_se_park
Asked:
esi_se_park
  • 5
  • 2
1 Solution
 
Chris DentPowerShell DeveloperCommented:

If we do UserProfile first. You won't be able to use it as a constant anymore, but this should work.

Chris
Dim objShell : Set objShell = CreateObject("WScript.Shell")
Dim USER_PROFILE : USER_PROFILE = objShell.ExpandEnvironmentStrings("%UserProfile%")
Dim BACKUP_LOG_PATH : BACKUP_LOG_PATH = USER_PROFILE & "\Local Settings\Application Data\Microsoft\Windows NT\NTBackup\data"

Open in new window

0
 
Chris DentPowerShell DeveloperCommented:

And for the log file, we could do this.

Chris
Sub EmailLog(logFilePath)
        ' Open the Text File and read the contents
        Set objFile = objFSO.OpenTextFile(BACKUP_LOG_PATH & "\" & logFilePath, 1, False, 0)
        strFile = objFile.ReadAll
 
        Set oCDO = CreateObject("CDO.Message")
 
        oCDO.Subject = "Backup Log - " & strComputerName
        oCDO.From = strComputerName & "@company.com"
        oCDO.To = EMAIL_RECIPIENT
 
        oCDO.TextBody = "Backup log attached for " & strComputerName & vbCrLf & vbCrLf & strFile
 
        oCDO.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
        oCDO.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") ="smtpserver"
        oCDO.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25 
        oCDO.Configuration.Fields.Update
        oCDO.Send
 
        Set oCDO = Nothing
End Sub

Open in new window

0
 
esi_se_parkAuthor Commented:
The %userprofile% section worked, but emailing the log file content gave me a VBScript runtime error: variable is undefined: 'objFSO'
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
Chris DentPowerShell DeveloperCommented:

Oops sorry, misread the reference to it earlier. Just replace "objFSO" with "oFSO".

We'll also need "Dim objFile" and "Dim strFile" or it'll give the same message for those.

Chris
0
 
esi_se_parkAuthor Commented:
We are so close.  Chris-Dent suggestion got rid of all the runtime errors, and it does send out the email.  Porblem is the email says "ÿþB".  The file is a .log file and if I use notepad, I can actually see the content, which is not "ÿþB".  
0
 
Chris DentPowerShell DeveloperCommented:

Unicode? I'm opening the file as ASCII at the moment.

Chris
0
 
Chris DentPowerShell DeveloperCommented:

We can test it actually. This modification switches to Unicode.

It's a modification to this line:

        Set objFile = objFSO.OpenTextFile(BACKUP_LOG_PATH & "\" & logFilePath, 1, False, -1)

The last value, -1, means use Unicode, the previous value, 0, means use ASCII.

Chris
Sub EmailLog(logFilePath)
        ' Open the Text File and read the contents
        Set objFile = objFSO.OpenTextFile(BACKUP_LOG_PATH & "\" & logFilePath, 1, False, -1)
        strFile = objFile.ReadAll
 
        Set oCDO = CreateObject("CDO.Message")
 
        oCDO.Subject = "Backup Log - " & strComputerName
        oCDO.From = strComputerName & "@company.com"
        oCDO.To = EMAIL_RECIPIENT
 
        oCDO.TextBody = "Backup log attached for " & strComputerName & vbCrLf & vbCrLf & strFile
 
        oCDO.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
        oCDO.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") ="smtpserver"
        oCDO.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25 
        oCDO.Configuration.Fields.Update
        oCDO.Send
 
        Set oCDO = Nothing
End Sub

Open in new window

0

Featured Post

Industry Leaders: 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!

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