Those Annoying thing in Powershell

Powershell v2.0 has a cmdlet that allows you to send an email

Send-MailMessage

NAME  
  Send-MailMessage

SYNOPSIS  
    Sends an e-mail message.

SYNTAX  
    Send-MailMessage [-To] <string[]> [-Subject] <string> -From <string> [[-Body] <string>] [[-SmtpServer] <string>] [-Attachments <string[  
    ]>] [-Bcc <string[]>] [-BodyAsHtml] [-Cc <string[]>] [-Credential <PSCredential>] [-DeliveryNotificationOption {None | OnSuccess | OnFailure | Delay | Never}] [-Encoding <Encoding>] [-Priority {Normal | Low | High}] [-UseSsl] [<CommonParameters>]

DESCRIPTION  
    The Send-MailMessage cmdlet sends an e-mail message from within Windows PowerShell.

RELATED LINKS  
  Online version: [https://go.microsoft.com/fwlink/?LinkID=135256](https://go.microsoft.com/fwlink/?LinkID=135256)

REMARKS  
    To see the examples, type: "get-help Send-MailMessage -examples".  
    For more information, type: "get-help Send-MailMessage -detailed".  
    For technical information, type: "get-help Send-MailMessage -full".

Ok so today I has a collection that I had stored in a variable

[12:39:03] ~> $myvar | gm

   TypeName: Selected.System.Management.Automation.PSCustomObject

And when I tried this:

[12:40:18] ~> Send-MailMessage From "[email protected]" -To "[email protected]" -Subject "test" -SmtpServer smtp.maishsk.local -Body ($myvar)

I was presented with this:

**Send-MailMessage : Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'Body'. Specified method is not supported.  
At line:1 char:114  
+ Send-MailMessage From [email protected] To [email protected] -Subject "test" -SmtpServer smtp.maishsk.local -Body <<<<  ($myvar)  
    + CategoryInfo          : InvalidArgument: (:) [Send-MailMessage], ParameterBindingException  
    + FullyQualifiedErrorId : CannotConvertArgument,Microsoft.PowerShell.Commands.SendMailMessage**

Thanks to my trusted friend Shay Levy (who always has the time to help out and is never tired of my questions - “I hope…” ) he explained to me why this was happening.

The object that is expected in the Body parameter is a TypeName: System.String object. I also could have seen that if I had looked properly in the help of the cmdllet – [[-Body] ] –

So the solution was very simple. Out-String

[12:49:11] ~> $myvar | Out-String | gm

   TypeName: System.String

And with that it was not a problem to send the mail

[12:50:18] ~> Send-MailMessage From "[email protected]" -To "[email protected]" -Subject "test" -SmtpServer smtp.maishsk.local -Body ( $myvar | out-string )

Is it not wonderful that you learn something new every day!

Back to work….

Small Edit:

<—— RANT ——–>

Just clarify this is not a Powershell problem – it does exactly what it is supposed to.
The annoying part is that it takes a while to figure out the problem is.

<—— END RANT—–>