Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save SteloNLD/8ade5b04b326cb437d5041e0a46f7fa1 to your computer and use it in GitHub Desktop.
Save SteloNLD/8ade5b04b326cb437d5041e0a46f7fa1 to your computer and use it in GitHub Desktop.
Powershell Example: HashTables, SecureString, Splatting, Send-MailMessage
### Script Global Settings
#Declare SMTP Connection Settings
$SMTPConnection = @{
#Use Office365, Gmail, Other or OnPremise SMTP Relay FQDN
SmtpServer = 'outlook.office365.com'
#OnPrem SMTP Relay usually uses port 25 without SSL
#Other Public SMTP Relays usually use SSL with a specific port such as 587 or 443
Port = 587
UseSsl = $true
#Option A: Query for Credential at run time.
Credential = Get-Credential -Message 'Enter SMTP Login' -UserName "[email protected]"
<#
#Option B: Hardcoded Credential based on a SecureString
Credential = New-Object -TypeName "System.Management.Automation.PSCredential" -ArgumentList @(
#The SMTP User Emailaddress
"[email protected]"
#The Password as SecureString encoded by the user that wil run this script!
#To create a SecureString Use the folowing Command: Read-Host "Enter Password" -AsSecureString | ConvertFrom-SecureString
"Enter the SecureString here as a single line" | ConvertTo-SecureString
)
#>
}
### Script Variables
#Declare Mailmessages.
$MailMessageA = @{
From = "[email protected]"
To = @(
"[email protected]"
)
#Cc = @(
# "[email protected]"
#)
#Bcc = @(
# "[email protected]"
#)
Subject = 'Mailmessage from script'
#Priority = 'Normal' #Normal by default, options: High, Low, Normal
#Attachments = @(
#'FilePath'
#)
#InlineAttachments = @{
#'CIDA'='FilePath'
#} #For more information about inline attachments in mailmessages see: https://gallery.technet.microsoft.com/scriptcenter/Send-MailMessage-3a920a6d
BodyAsHtml = $true
Body = "Something Unexpected Occured as no Content has been Provided for this Mail Message!" #Default Message
}
### Script Start
#Retrieve Powershell Version Information and store it as HTML with Special CSS Class
$PSVersionTable_HTLM = ($PSVersionTable.Values | ConvertTo-Html -Fragment) -replace '<table>', '<table class="table">'
#Retrieve CSS Stylesheet
$CSS = Invoke-WebRequest "https://raw.githubusercontent.com/advancedrei/BootstrapForEmail/master/Stylesheet/bootstrap-email.min.css" | Select-Object -ExpandProperty Content
#Build HTML Mail Message and Apply it to the MailMessage HashTable
$MailMessageA.Body = ConvertTo-Html -Title $MailMessageA.Subject -Head "<style>$($CSS)</style>" -Body "
<p>
Hello World,
</p>
<p>
If your recieved this message then this script works.</br>
</br>
<div class='alert alert-info' role='alert'>
Powershell version
</div>
$($PSVersionTable_HTLM)
</P>
" | Out-String
#Send MailMessage
#This example uses the HashTable's with a technique called Splatting to match/bind the Key's in the HashTable with the Parameters of the command.
#Use the @ Symbol instead of $ to invoke Splatting, Splatting improves readability and allows for better management and reuse of variables
Send-MailMessage @SMTPConnection @MailMessageA
@SteloNLD
Copy link
Author

InlineAttachments requires a custom Send-MailMessage command wich you can find here: https://gallery.technet.microsoft.com/scriptcenter/Send-MailMessage-3a920a6d

@SteloNLD
Copy link
Author

SteloNLD commented Apr 19, 2018

BodyAsHTML = $True allows you to do 3 things:

  1. Use simple HTML Code to style and structure your Mail Messages.
  2. Use CSS Code such as Bootstrap For Email to style your Mail Messages
  3. Allows you to properly display powershell object(s) in mail messages as a table by using $Object | ConvertTo-Html -Fragment

image

To further enhance your HTML Mail Messages please take a look at Invoke-PsHTML, this allows you to create HTML Template Files with inline Powershell Code, a proper template can be found at Bootstrap For Email

Contact me at [email protected] if you are interested in amping up your Powershell Reports / Mail Messages

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment