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.
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.
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