I'm trying to send email to multiple recipients in PowerShell.
I know about the PowerShell 1.0 way but I do not want to use it.
My email function looks like this
function Send-Mail() {
Param (
# email attributes
[string]$smtpServer,
[string]$from,
[string[]]$to,
[string]$subject,
[string]$body
)
Send-MailMessage -To $to -Subject $subject -BodyAsHtml $body -SmtpServer $smtpServer -From $from
}
I can achieve what I want doing this:
$smtpServer = "email.server.local"
$from = "[email protected]"
$subject = "Subject"
$body = "Test"
Send-Mail $smtpServer $from ("[email protected]", "[email protected]") $subject $body
..but if I put
$to = "[email protected]", "[email protected]"
Send-Mail $smtpServer $from $to $subject $body
Mail only goes to the second recipient
If I set $to locally in the function, this also works correctly, so the problem is passing the parameter to the function.
Get-Host. I don't see and issue here with your second example and this is working for me.$tostrongly cast in your current session at some point?$to.GetType().fullnamesystem.object[]orsystem.string[]. Save your code and reopen ISE / PS with a new session. Does it work then? If you strongly cast the variable you would not be able to change it later without first removing it.Remove-Variable torm variable:to. Have just re-run the code and it's working. Thanks a lot as this has bugged me out since last night. Phew! Cheers.