I live in the South, specifically in God’s country AKA TEXAS BABAY! There’s plenty of amazing things about Texas and definitely some reasons to loathe being from Houston, but it’s hard to knock our food. I mean, is there really even a debate between Whataburger vs In-N-Out? I’ll save you a google search (or bing or asking Jeeves or however you live your life): the answer is no. Long live the Whataburger! Tex-Mex is another blessed invention from the south and it’s phenomenal. If you’re from here, Tex-Mex is most likely a part of the lowest level of Maslow’s Hierarchy of Needs for your survival and wellbeing. You’ve replaced all bland, tasteless food in your life with a kick in the mouth of spicy seasoning and flavor for days. The only issue is once you’ve incorporated all of this extra flavor into your diet, it will inadvertently spill over into your tech life. No longer can you bear to send emails from PowerShell that are bland and tasteless. Your tastebuds now crave rich emails with cavernous colors and all of the flavorful formatting that HTML has to offer. Well, let’s satisfy your hunger with a Snickers by converting our objects into tables, adding some CSS styling, and building a function to give us a spicy status bar that even Outlook will like. Let’s spice up those HTML Emails with PowerShell!
Wait, before we even get out all of our ingredients, I need you to do something for me. Stop what you’re doing and tweet Chuy’s for me and tell them to bring back the Elvis Green Chile Fried Chicken please.
Did you do it???
You’re the best! And trust me, if you never had Elvis Green Chile Friend Chicken from Chuy’s while they were serving them…your life has a hole in it you didn’t even know about. Thought you were dropped on your head as a child? No ma’am, this is the reason you are the way you are today.
Dear Chuy's, it's me again. Just wanted to bump this request to the top of your 2022 plans. If you need me to sit in on any meetings or attend a planning retreat, I'm here for it!— Todd Willett (@ThatWillett) March 11, 2022
\\\You’ve Got Mail
Okay, now let’s take a look at sending a basic email in PowerShell using Send-MailMessage. Yeah, yeah, I know it’s technically being phased out but I’ll keep using it until they rip it from my cold dead hands! The basics of sending an email (assuming you have an SMTP relay already configured and working) is:
To, From, Subject, Body, SmtpServer, Port.
There are some others depending on your needs, but let’s just go over sending a simple email if you haven’t done that before. An example looks like this:
Send-MailMessage -To 'ChefBoyardee@food.edu' -From 'Ravi@food.edu' -Subject 'Dinner Time!' -Body "It's dinner time amico!" -SmtpServer server1.food.edu -Port 25
So your port might be different if you’re using SSL or you might have to authenticate to your relay depending on if it’s internal and what security measures you have in place, but you get the idea. And yes Karen, I know Send-MailMessage is obsolete but they can pry it from my cold, dead hands!
Okay, </rant>. Let’s get back in the kitchen. These types of emails work for those of us with no personality and no friends, but the rest of the world is accustomed to information being a little easier on the eye. Especially if we wanted to send some actual data in this email…
What if we wanted to get the MAC Address of our network interfaces and include the object in the body of the email?
We could try:
$Adapters = Get-NetAdapter | select Name, MacAddress, MediaConnectionState $Body = "It's dinner time amico! And oddly, here is the network adapter information you wanted:" + $Adapters Send-MailMessage -To 'ChefBoyardee@food.edu' -From 'Ravi@food.edu' -Subject 'Dinner Time!' -Body $Body -SmtpServer server1.food.edu -Port 25
\\\Two Tbps of Conversion
Thankfully, we’re given a cool tool to convert our
$Adapters object to HTML with the
$Adapters | ConvertTo-HTML
<tr>(Table Rows) and
<td>(Table Data) tags. If we only want to send the object with no other content in the body, this works. But if we want the table without the
<body>tags so we can add it ourselves, use the
$Adapters | ConvertTo-HTML -Fragment
-Fragmentswitch, we can add other elements into the body of our email and utilize HTML to take it up a notch. Let’s add in some more text with a count of the adapters and make the number bold. Maybe include a link to a great recipe that pairs really nicely with network adapters. We want to make sure to wrap our body (not physically, of course) in an HTML tag.
$Body = '' $Body += "It's dinner time amico! And oddly, you have <b>$($Adapters.count) network adapters.</b> Here is the info:<br><br>" $Body += $Adapters_HTML $Body += "<br>Oh yeah! I almost forgot, here's a great recipe to make your own <a href=`"https://www.modernhoney.com/chuys-creamy-jalapeno-cilantro-dip-dressing`">Chuy's Creamy Jalapeno Sauce!</a>"
<a>for a link,
<br>for a line break. Let’s take a whiff at this and smell what we have cookin’:
It’s definitely starting to get saucy. We’re well on our way to crafting a delcious email that’s sure to please the palate. In Part II, we’ll take what we’ve been building and spice it up with a graphic, a footer, and some sections to better visualize our masterpiece. Stay saucy my friends!