eodeluga
eodeluga

Reputation: 698

Passing multiple strings as single function parameter

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.

Upvotes: 0

Views: 1460

Answers (3)

Lord Helmet
Lord Helmet

Reputation: 170

I sent it like this and it worked like a charm. PS v4, Windows Server 2012

Send-MailMessage -To "[email protected]", "[email protected]" -Body "TEST" -From "[email protected]" -Subject "TEST" -SmtpServer smarthost.domain.com

for your function purposes, I would submit the emails as an array. This also worked for me.

$array = New-Object System.Collections.ArrayList
$array += "[email protected]"
$array += "[email protected]"
Send-MailMessage -To $array -Body "TEST" -From "[email protected]" -Subject "TEST" -SmtpServer smarthost.domain.com

...and ultimately, this worked for me just fine using your function.

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 
}
$array  = New-Object System.Collections.ArrayList
$array += "[email protected]"
$array += "[email protected]"
Send-Mail -smtpServer "smarthost.domain.com" `
          -from       "[email protected]"      `
          -to         $array                 `
          -subject    "Test"                 `
          -body       "TEST"

Upvotes: 0

Matt
Matt

Reputation: 46710

The code you have listed here should have worked just fine. PowerShell is rather forgiving when it comes to typing and it will make the best selection for you. However you are able to cast yourself as [string] for example which I suspect your did at some point in your session before you hosted. Consider the following examples

PS C:\Users\matt> $to = "[email protected]", "[email protected]"

PS C:\Users\matt> $to.GetType().Fullname
System.Object[]

PS C:\Users\matt> [string]$to = "[email protected]"

PS C:\Users\matt> $to.GetType().Fullname
System.String

PS C:\Users\matt> $to = "[email protected]", "[email protected]"

PS C:\Users\matt> $to.GetType().Fullname
System.String

Note that in the last set you might have expected System.Object[] but since it was strongly cast in an earlier line that it remains that type until it is removed.

You can also see this when examining the variable

PS C:\Users\mcameron> Get-Variable to | fl

Name        : to
.... output truncated ...
Attributes  : {System.Management.Automation.ArgumentTypeConverterAttribute}

Key point here is the Attribute System.Management.Automation.ArgumentTypeConverterAttribute which showed up after the strong cast. During a session or during code execution you can simple remove the variable with Remove-Variable to which would let you start over.

Upvotes: 1

It-Z
It-Z

Reputation: 2000

$to = @("[email protected]", "[email protected]") will set $to to be an array

Upvotes: 0

Related Questions