Sending reports by e-mail

After creating a report, you might want to send it to your boss or to yourself. The PowerShell Send-MailMessage cmdlet can send e-mail messages using an SMTP server. The syntax of this cmdlet is as follows:

Send-MailMessage [-To] <String[]> [-Subject] <String> [[-Body]  <String>] [[-SmtpServer] <String>] [-Attachments <String[]>] [-Bcc  <String[]>] [-BodyAsHtml] [-Cc <String[]>] [-Credential  <PSCredential>] [-DeliveryNotificationOption  {None | OnSuccess | OnFailure | Delay | Never}] [-Encoding <Encoding>] [-Port <Int32>] [-Priority {Normal | Low | High}] [-UseSsl] -From <String> [<CommonParameters>]

The -From, -To, and -Subject parameters are required to send a report by e-mail. You can use the PowerShell $PSEmailServer preference variable for the SMTP server. If the $PSEmailServer variable is not set, you have to use the -SmtpServer parameter.

You can send a report by putting it in the body of the e-mail or as an attachment. If the report is an HTML document and you want to send it in the body of the e-mail, you have to use the -BodyAsHtml parameter.

In the first example, the HTML report file that was created in the preceding section, Generating HTML reports, is sent in the body of an e-mail. In this example, we will use splatting to specify the parameters. The PowerShell Out-String cmdlet is used to create a single string from the HTML content.

The SMTP server used in this environment does not require authentication or SSL and uses the default port 25. Additional parameters may be required in some environments:

$Parameters = @{ 
  From = '[email protected]' 
  To = '[email protected]' 
  Subject = 'VMware vSphere hosts Connection State report' 
  Body = Get-Content -Path 'c:VMHostsConnectionState.html' | 
         Out-String 
  BodyAsHtml = $true 
  SmtpServer = 'smtpserver.blackmilktea.com' 
} 
Send-MailMessage @Parameters 

Tip

You don't have to create a file before you can send an e-mail. You can also use the content of a variable.

In the second example, we will use the content of the $HTML variable (created in the preceding section, Generating HTML reports ) as the body of the e-mail:

$Parameters = @{ 
  From = '[email protected]' 
  To = '[email protected]' 
  Subject = 'VMware vSphere hosts Connection State report' 
  Body = $HTML 
  BodyAsHtml = $true 
  SmtpServer = 'smtpserver.blackmilktea.com' 
} 
Send-MailMessage @Parameters 

In the third example, we will send the HTML report created in the preceding section,  Generating HTML reports as an attachment:

$Parameters = @{ 
  From = '[email protected]' 
  To = '[email protected]' 
  Subject = 'VMware vSphere hosts Connection State report' 
  Body = 'VMware vSphere hosts Connection State report is attached
  to this email.' 
  Attachment = 'c:VMHostsConnectionState.html' 
  SmtpServer = 'smtpserver.blackmilktea.com' 
} 
Send-MailMessage @Parameters 
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.45.162