Skip to content
Menu
The Lonely Administrator
  • PowerShell Tips & Tricks
  • Books & Training
  • Essential PowerShell Learning Resources
  • Privacy Policy
  • About Me
The Lonely Administrator

Send PowerShell Reports by Mail

Posted on December 29, 2011

Today I came across a post about pinging a list of computers and send the results as a table via Outlook. Brian is a very smart Active Directory guy so I offered some PowerShell suggestions to make his task even easier. Obviously you can only offer so much in a blog comment, so I thought I'd drop a few more details here.

Manage and Report Active Directory, Exchange and Microsoft 365 with
ManageEngine ADManager Plus - Download Free Trial

Exclusive offer on ADManager Plus for US and UK regions. Claim now!

I'm going to follow Brian's original intent, but you can take these ideas and apply them to other tasks you might accomplish in PowerShell. First, let's ping. I prefer to use the Test-Connection cmdlet. The original need was for the host name and its IP address. Assuming local hosts that should be running, I can get by with a command like this"


Test-Connection "server1","server2" -count 1

This will ping the two servers once. It would be nice if Test-Connection accepted pipelined input by value, but it only does by property name. So to ping a list of computers I can use either of these approaches.


get-content computers.txt | foreach { test-connection $_ -count 1}
#or
Test-Connection -computer (get-content computers.txt) -count 1

Personally, I find the latter a bit easier. Next we only want certain properties. Piping output to Select * helps me identify the properties.


Test-Connection -computer (get-content computers.txt) -count 1 | Select Address,IPv4Address

That will be the body of the mail message which we'll send using the Send-MailMessage cmdlet. I intend to send an HTML formatted mail message so I'll take my Test-Connection command and run it through ConvertTo-HTML to create the code.


[string]$body=Test-Connection -computer (get-content computers.txt) -count 1 | Select Address,IPv4Address | ConvertTo-HTML

The body value must be a string so I'm casting $body to treat the output as one long string. Now to send the message.


send-mailmessage -to [email protected] -subject "Ping Report" -from "[email protected]" -body $body -bodyasHTML -smtpserver chi-ex01.globomantics.local

Here's what the email looks like in OWA.

If I had a network accessible style sheet, I could have included a reference in the HTML code to make it even prettier. But perhaps you don't or can't send HTML. We could go this route. First, let's get the body text.


$body2=test-connection -computer (get-content computers.txt) -count 1 | select Address,IPv4Address

Notice I didn't cast $body2 as a string. I wanted the variable to hold the collection of objects. Perhaps I'll want to work with it latter. I'll turn it into a string in the Send-MailMessage command.


send-mailmessage -to [email protected] -subject "Ping Report" -from "[email protected]" -body ($body2 | out-string) -smtpserver chi-ex01.globomantics.local

For the body, I took the variable and piped it to Out-String. Here's the email. It is still a decent looking table.

Not quite as nice because it doesn't used a fixed width font. But it would do in a pinch. Or I could have saved my ping results to a file and sent it as an attachment. I'll let you figure that out.

If you'd like to see more ways of using PowerShell to build reports or gather data, I hope you'll take a look at my video training course, Windows Server 2008 PowerShell Training from TrainSignal.


Behind the PowerShell Pipeline

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on Mastodon (Opens in new window) Mastodon
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to share on Pocket (Opens in new window) Pocket
  • Click to share on Reddit (Opens in new window) Reddit
  • Click to print (Opens in new window) Print
  • Click to email a link to a friend (Opens in new window) Email

Like this:

Like Loading...

Related

1 thought on “Send PowerShell Reports by Mail”

  1. Claus T Nielsen says:
    December 30, 2011 at 6:08 am

    Here is an example of adding a little bit of HTML tables to output the result:


    $HTMLStyle = "

    "

    [string]$body = Test-Connection -computer (get-content computers.txt) -count 1 | Select Address,IPv4Address | ConvertTo-Html -Head $HTMLStyle

    send-mailmessage -to [email protected] -subject "Ping Report" -from "[email protected]" -body $body2 -bodyasHTML -smtpserver chi-ex01.globomantics.local

Comments are closed.

reports

Powered by Buttondown.

Join me on Mastodon

The PowerShell Practice Primer
Learn PowerShell in a Month of Lunches Fourth edition


Get More PowerShell Books

Other Online Content

github



PluralSightAuthor

Active Directory ADSI Automation Backup Books CIM CLI conferences console Friday Fun FridayFun Function functions Get-WMIObject GitHub hashtable HTML Hyper-V Iron Scripter ISE Measure-Object module modules MrRoboto new-object objects Out-Gridview Pipeline PowerShell PowerShell ISE Profile prompt Registry Regular Expressions remoting SAPIEN ScriptBlock Scripting Techmentor Training VBScript WMI WPF Write-Host xml

©2025 The Lonely Administrator | Powered by SuperbThemes!
%d