0
votes

I understand that there are multiple blogs which teaches how to send email with html and css formatting but nothing seems to work in PowerShell Workflow in Azure Runbooks.

I want to send status of a scheduled Azure Runbook which processes an Azure Analysis Tabular Model in an email with table having all the details.

As of now, I'm trying to send an email with basic borders. So far I have used this script:

<# 
	This PowerShell script was automatically converted to PowerShell Workflow so it can be run as a runbook.
	Specific changes that have been made are marked with a comment starting with “Converter:”
#>

workflow TablePS {
	
inlineScript {

# Create a DataTable
$table = New-Object system.Data.DataTable "TestTable"
$col1 = New-Object system.Data.DataColumn Name,([string])
$col2 = New-Object system.Data.DataColumn Dept,([string])
$table.columns.add($col1)
$table.columns.add($col2)

# Add content to the DataTable
$row = $table.NewRow()
$row.Name = "John"
$row.Dept = "Physics"
$table.Rows.Add($row)
$row = $table.NewRow()
$row.Name = "Susan"
$row.Dept = "English"
$table.Rows.Add($row)

# Create an HTML version of the DataTable
$html = "<table><tr><td>Name</td><td>Dept</td></tr>"
foreach ($row in $table.Rows)
{ 
    $html += "<tr><td>" + $row[0] + "</td><td>" + $row[1] + "</td></tr>"
}
$html += "</table>"
       
       $MyCredential = "DevCreds" 
       $Cred = Get-AutomationPSCredential -Name $MyCredential 
       $CredUsername = $Cred.UserName 
        $CredPassword = $Cred.GetNetworkCredential().Password 

# Send the email
$smtpserver = "smtp.office365.com"
$from = "[email protected]"
$to = "[email protected]"
$subject = "test"
$body = "Hi there,<br />Here is a table:<br /><br />" + $html
Send-MailMessage -smtpserver $smtpserver -UseSsl  -Port 587 -from $from -to $to -subject $subject -body $body -Credential $Cred  -bodyashtml

}
}

But this doesn't help me. I get a table type format without any borders. Can you guys please help me on this. PFB: Image Snippet Here

1

1 Answers

0
votes

When sending emails, remember that your CSS styles should be inline to support the majority of mail clients.

# Define your styles.
$borderWidth = "1px"
$borderStyle = "solid"
$borderColor = "rgba(255, 0, 0, 1)"
$borderStyles = @("border-width: $borderWidth;", "border-style: $borderStyle;", "border-color: $borderColor;")
$styleAttribute = " style=`"$borderStyles`""

Then just add your $styleAttribute to wherever you want to apply it.

# Create an HTML version of the DataTable
$html = "<table $styleAttribute><tr><td $styleAttribute>Name</td><td $styleAttribute>Dept</td></tr>"
foreach ($row in $table.Rows) { 
    $html += "<tr><td $styleAttribute>" + $row[0] + "</td><td $styleAttribute>" + $row[1] + "</td></tr>"
}
$html += "</table>"

Play around with the CSS until you get what you need. You'll want to add styling to your table and td elements.