Jump to content
Software FX Community

Send-Mail tip


SAPIENScripter
 Share

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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...