ps/Modules/Alkami.DevOps.Common/Public/Send-ObjectAsHtmlTable.ps1

74 lines
3.1 KiB
PowerShell
Raw Normal View History

2023-05-30 22:51:22 -07:00
function Send-ObjectAsHtmlTable {
<#
.SYNOPSIS
Sends a formatted email representing a given powershell object.
.DESCRIPTION
Translates a powershell object into an html table, keeping the ordering of the
properties (if they were ordered). Joins the object html to an html template
located in the modules file list and emails it to the specified recipients.
.PARAMETER ToAddress
[string[]]One or more email address to send the powershell object to.
.PARAMETER FromAddress
[string]The from address for this email, defaults to 'SreAlerts@alkamitech.com'
.PARAMETER Subject
[string] The subject of the email you wish to send
.PARAMETER SMTPUsername
[string] The username which has access to the mail server.
.PARAMETER SMTPPassword
[string] The password for the user that has access to the mail server.
.PARAMETER SMTPServer
[string] The url of the SMTPServer
.EXAMPLE
Send-ObjectAsHtmlTable -ToAddress "toUser@alkamitech.com" -Subject "Something Happened" -InputObject (get-process | Select-Object -First 1 Handles,ProcessName) `
-SMTPUsername "user@smtp.com -SMTPPassword "anSmtpPassword" -SMTPServer "smtp.someServer.org"
Will translate the input object and converting it's properties, in this case 'Handles' and 'ProcessName' into fields for an html table.
Then join the html table to an email template and send to the user specified in the to address.
#>
[CmdletBinding()]
param(
[Parameter(Mandatory=$True)]
[string[]]$ToAddress,
[Parameter()]
[string]$FromAddress = "SreAlerts@alkamitech.com",
[Parameter(Mandatory=$True)]
[string]$Subject,
[Parameter(Mandatory=$True)]
[object[]]$InputObject,
[Parameter(Mandatory=$True)]
[string]$SMTPUsername,
[Parameter(Mandatory=$True)]
[SecureString]$SMTPPassword,
[Parameter(Mandatory=$True)]
[string]$SMTPServer
)
begin{
$Credential = New-Object -TypeName "System.Management.Automation.PSCredential" -ArgumentList $SMTPUsername, $SMTPPassword
if(!$Credential){throw "Credentials for sending mail message could not be created"}
$HtmlTemplatePath = $MyInvocation.MyCommand.Module.FileList | Where-Object {$_ -match "ObjectAsTableTemplate.html"}
if(!(Test-Path $HtmlTemplatePath)){throw "HTML Template could not be found at path $HtmlTemplatePath"}
$HtmlTemplate = Get-Content $HtmlTemplatePath | Out-String
}
process{
$headers = $InputObject | Select-Object -first 1 | ForEach-Object {$_.psobject.Properties | ForEach-Object {$_.Name}}
$width = "width:$(100/ $headers.Count)%"
$headerData = $headers | ForEach-Object {"<th style=""text-align: center;$width"">$_</th>"}
$bodyData = $InputObject | ForEach-Object {"`n<tr>";$obj = $_;$headers | ForEach-Object {"`n<td>$($obj.$_)</td>"};"`n</tr>"}
$HtmlTemplate = $HtmlTemplate.Replace("[TableHeaderData]",$headerData)
$HtmlTemplate = $HtmlTemplate.Replace("[TableBodyData]",$bodyData)
Send-MailMessage -Body $HtmlTemplate -BodyAsHtml -from $FromAddress -To $ToAddress -Subject $Subject -UseSsl -Credential $Credential -Port 587 -SmtpServer $SMTPServer
}
}