Video Screencast Help
Scheduled Maintenance: Symantec Connect is scheduled to be down Saturday, April 19 from 10am to 2pm Pacific Standard Time (GMT: 5pm to 9pm) for server migration and upgrade.
Please accept our apologies in advance for any inconvenience this might cause.

How to send BEMCLI results as e-mails

Created: 04 Jul 2012 • Updated: 04 Aug 2012
Language Translations
pkh's picture
+1 1 Vote
Login to vote

When you run BEMCLI cmdlets, you can direct the results to a file on disk, e.g.

Get-BEJobHistory > \jobhistory.txt

This is useful when you schedule a script or the script is a long running one.  For example, when you chain your jobs and want to see the job history at the end of the run.  See my article

https://www-secure.symantec.com/connect/articles/use-bemcli-chain-jobs

https://www-secure.symantec.com/connect/forums/script-chaining-jobs-be-2012

However, it would be nice to get the results by e-mail so you do not have to logon to the media server to check the results file.  This can be done since BEMCLI is a Powershell module and there is a Powershell facility to send e-mails.

I have attached a working Powershell script to send all the job history and the corresponding job logs to somebody via Gmail.  This script is an example of how to send BEMCLI results by e-mail.  The various sections also show how rich Powershell is for manipulating objects.  To run the script, change the To and From fields with some actual e-mail addresses and also put in a valid Gmail userid and password in the SMTP client portion of the script.  Before running the script, you may need to prepare your Powershell environment.  See my article

https://www-secure.symantec.com/connect/articles/preparing-your-powershell-environment-run-bemcli-and-scripts

You can, of course, tailor the script to meet your own needs or use some other BEMCLI cmdlets.

 

I would like to go through each of the lines in the script briefly.

Import-Module "\program files\symantec\backup exec\modules\bemcli\bemcli"

This line imports the BEMCLI module.  If you have included the import cmdlet in your profile, then you can comment out this line.
 
$style = "<style>BODY{font-family: Verdana; font-size: 11pt;}"
$style +=  "TABLE{border: 1px solid black; border-collapse: collapse;}"
$style +=  "TH{border: 1px solid black; background: #dddddd; padding: 5px; }"
$style +=  "TD{border: 1px solid black; padding: 5px; } </style>"
 
These lines are to set the style for the body of the e-mail
 
$message = New-Object Net.Mail.MailMessage
 
This line creates a new mail message object.
 
$message.From = "alias@domainname"
 
This is the From field of the e-mail.  You need to put in a valid e-mail address.
 
$message.To.Add("alias@domainname")
# $message.Cc.Add("alias@domainname")
# $message.Bcc.Add("alias@domainname")
 
The above lines build up the To, CC and BCC list of receipients for this e-mail.  For each additional receipient, cc or bcc, do another Add.
 
$message.Subject = "BE Job History"
 
This is the subject field of the e-mail.
 
$message.IsBodyHtml = $true;
 
This is say that the body of the e-mail is in HTML format. If you are sending just text, you can leave out this line.
 
Here we create the body of the e-mail
 
$JobHistory = Get-BEJobHistory
 
In this example, I get all the job history.  You can select specify categories, e.g. missed jobs
 
$JobHistory = Get-BEJobHistory -JobStatus Missed
 
 
$message.Body = $JobHistory | ConvertTo-Html -Head $style
 
This line converts the job history into a HTML table for the e-mail body.
 
You can attach the joblogs as they are to the mail as attachments, but they are XML files and are not easy to read.  I have included this section as an example of how to attach an existing file to a mail as an attachment.
 
# $joblogs = $JobHistory | Where-Object {!($_.JobLogFilePath -like '')}
 
# Foreach ($log in $joblogs)  {
 
#   $att = new-object Net.Mail.Attachment($log.JobLogFilePath)
#   $message.Attachments.Add($att)
#  }
 
For each mail attachment, a new mail attachment object needs to be created and then add it to the mail message object created earlier.
 
It would be better to attach the joblogs in HTML format which is what the next section does.
 
Foreach ($log in $JobHistory)  {
 
   $attName = "Joblog - " + $log.Name + ".htm"
   $attText = $log | Get-BEJobLog -OutputStyle html   
   $att = [System.Net.Mail.Attachment]::CreateAttachmentFromString($attText,$attName) 
   $message.Attachments.Add($att)
  }
 
The second line in the loop converts the joblog to HTML format and store it as a string.  The next line then instantiate the attachment object from this string, so you do not have to write it out to a file.
 
 
$SMTPServer = "smtp.gmail.com"
 
In this example, I use Gmail to send the e-mail. If you are using some other SMTP server, put in the correct URL.
 
Now to set up the SMTP client.  The Gmail SMTP server uses SSL authentication, so you need to have a valid Gmail account to send the e-mail.  If you are using some other SMTP server or Exchange, then you would need to set up the SMTP client according to the security settings required.
 
$SMTPClient = New-Object Net.Mail.SmtpClient($SMTPServer, 587)
 
This line creates a new SMTP client and use port 587 whiich is required by Gmail
 
$SMTPClient.EnableSsl = $true
 
Gmail uses SSL authentication.
 
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential("<userid>", "<password>");
 
This line establishes the credentials of the SMTP client.  You would need to change the userid and password to a valid Gmail account.
 
 
$SMTPClient.Send($message)
 
Finally, send the e-mail. If you are seding multiple e-mails, you only need to create the SMTP client once.