One script that I wrote, that I make somewhat frequent use of, is my “Send-HTMLEmail” script. It’s simple, and makes sending an Object out via email (example: a reporting/logging object) pretty easy to do.
This script takes a few parameters as input, which I’ll step you through.
.PARAMETER InputObject (mandatory)
Any PSOBJECT or other Table. Many of the “Get-” cmdlets in PoSh return some sort of PSObject, which can be sent to this script as $InputObject. One example may be if you do something like “Get-Service”, and want to return all services that have a running status, and email that list out.
.PARAMETER Subject (mandatory)
The Subject line of the email
The To field is who receives the email. The reason I did not make this mandatory is because in my cases, this script was always going to email the same distribution group (hence, I set a default value). In the rare cases where I need to email someone else, I just pass the parameter.
The From address of the email – same idea as the To parameter, where a different value can be passed, but my scripts are sent from a single address (which can make setting up rules in outlook easier)
This is the Cascading Style Sheet that will be used to Style the table. There is a functional but simple CSS included by default. If you want to override the styling, you can pass in your own CSS here. It’ll take any valid or invalid CSS, and does no checking. If you’re going to be using a lot of CSS, I recommend updating the script and placing it all in there, rather than trying to pass it as a parameter.
The SMTP relay server – statically set, but some situations may call for one-off cases of using a different server. In my case, I just use a VIP.
That’s all there really is to the parameters!
One thing I keep changing in the script is how the body of the email is handled. For now, I’ve settled on keeping the header of the report generic. I’ll likely revisit this down the line and make it a bit more interactive and dynamic. For now, it serves the function. If you’d like to change how that works, you just need to update this block:
The string following the Body (line 94) is the top line in the email body. and the PostContent string is what will follow the PSObject. As you can see, you can edit these lines with HTML and using your own or the included CSS.
That’s it! As I mentioned before, it’s a simple script, just used to send out tables of information from other scripts. You can get this script here: Send-HTMLEmail.