Jump to content
Software FX Community

Send-Mail tip


SAPIENScripter

Recommended Posts

I was testing out Send-Mail and came across something I thought I'd share as a tip. If you are piping the output of a cmdlet straight to Send-Mail, you might not get the result you expect.  If you run a command like get-process |Send-mail, the resulting mail message will look like this:

Microsoft.PowerShell.Commands.Internal.Format.FormatStartDataMicrosoft.PowerShell.Commands.Internal.Format.GroupStartDataMicrosoft.PowerShell.Commands.Internal.Format.FormatEntryDataMicrosoft.PowerShell.Commands.Internal.Format.FormatEntryDataMicrosoft.PowerShell.Commands.Internal.Format.FormatEntryData
You have to remember to pipe the output to Out-String and then to Send-mail 
get-process | Out-String | Send-Mail
Unless Send-mail is tweaked so that any object passed to it is converted to a string so you don't have to think about it. 

 

Link to comment
Share on other sites

We have an implementation that follows your recommendation, it accepts both

get-process | send-mail

get-process | out-string | send-mail

There is an interesting scenario we wanted to share with you, what would you expect is the output of the following command

dir *.csv | send-mail

a) Is it an email with the text output generated by get-childitem

B) Is it an email that includes the csv as attachments?

I am guessing you can say "give me both and a parameter that specifies which method to use" but we want to know which is the behavior you expect to be the default. Obviously if you do "dir *.csv | out-string | send-mail" we will send the output generated by get-childitem as we will not get the real files.

JuanC

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...