Email Array of Objects Using PowerShell

I use a couple different email functions to send different reports. This can be very useful to get script results back when you setup PowerShell based scheduled tasks.  This particular function adds an HTML header that formats the table a little nicer than the ConvertTo-HTML cmdlet does on its' own. It then sends out an HTML formatted email which. You just need to pass an array of objects to the function along with the normal email type information. (NOTE: using Select-Object to specify exactly what you want will help with the clarity of the email) Here is an example where I'm emailing myself the list of processes currently running on a system

Example Usage:

Untitled - PowerGUI Script Editor
$to = "example@mydomain.com"
$from = "PowerShellScript@mydomain.net"
$subject = "List of Running Processes on " + $env:COMPUTERNAME
$smtp = "mySmtpRelay.mydomain.net"
$content = get-process | select processname,id
Send-EmailHTML -To $to -From $from -Subject $subject -SMTPServer $smtp -BodyAsArray $content

Resulting Email:

1

Function Code:

Send-EmailHTML

Untitled - PowerGUI Script Editor
function Send-EmailHTML {
<#
.SYNOPSIS
Sends HTML Formatted Email
.DESCRIPTION
Send-EmailHTML will take an array of objects and emails a table in HTML 
.EXAMPLE
$to = "example@mydomain.com"
$from = "PowerShellScript@mydomain.net"
$subject = "List of Running Processes on " + $env:COMPUTERNAME
$smtp = "mySmtpRelay.mydomain.net"
$content = get-process | select processname,id
Send-EmailHTML -To $to -From $from -Subject $subject -SMTPServer $smtp -BodyAsArray $content

DESCRIPTION
-----------
This will email a table of all running processes on the system returned from the Get-Process 
cmdlet.  Using a select statement returns the information with clarity
#>
    [CmdletBinding()] param (
        [parameter(Mandatory=$true)] [string]$To,
        [parameter(Mandatory=$true)] [string]$From,
        [parameter(Mandatory=$true)] [string]$Subject,
        [parameter(Mandatory=$true)] [string]$SMTPServer,
        [parameter(Mandatory=$true)] [array]$BodyAsArray,
        [parameter(Mandatory=$false)] [string]$CC,
        [Parameter(Mandatory=$false)] [string]$BCC
    )
    PROCESS {

        #html style definition
        $htmlHeader = @'
            <style>
                body { background-color:#FFFFFF; } 
                body,table,td,th { font-family:Tahoma; color:Black; Font-Size:10pt } 
                th { font-weight:bold; background-color:#1F497D; color:White } 
                td { background-color:#DDDDDD; }
            </style>
'@
        try {
            #convert array into HTML fragment string
            $htmlContent = $BodyAsArray | ConvertTo-Html -Fragment | Out-String
            
            #initialize mail object
            $mail = new-object System.Net.Mail.MailMessage
            $mail.from = $From
            $mail.to.add($To)
            if ($CC) { 
                $mail.cc.add($CC) 
            }
            if ($BCC) { 
                $mail.bcc.add($BCC) 
            }
            $mail.subject = $Subject
            
            #add html to mail body
            $html = [System.Net.Mail.AlternateView]::CreateAlternateViewFromString( ($htmlHeader + $htmlContent), $null, "text/html" )
            $mail.AlternateViews.Add($html)
            $mail.IsBodyHtml = 1
            
            #send mail
            $smtpClient = new-object System.Net.Mail.SmtpClient
            $smtpClient.Host = $SMTPServer
            $smtpClient.Send($mail)
        }
        catch {
            Write-Host "`n" $_.Exception.Message "`n" -ForegroundColor Magenta
            return
        }
    }    
}
Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>