Help with creating a function to allow for multiple string input on a parameter

Hello,

I was going through the process of setting up a new module, and with a lot of the work I do, I schedule emailed reports via powershell quite often. So I wanted to create a function that allows me to add a -replyTo parameter on emails.

function SendEmail {
    [CmdletBinding()]
    [OutputType()]
    param (
        [Parameter(Mandatory = $true)]
        [string]$EmailTo,      
        [Parameter(Mandatory = $true)]
        [string]$EmailFrom,      
        [Parameter(Mandatory = $false)]
        [string]$ReplyTo,      
        [Parameter(Mandatory = $false)]
        [string]$EmailCC,      
        [Parameter(Mandatory = $false)]
        [string]$EmailAttachment,      
        [Parameter(Mandatory = $True)]
        [string]$EmailBody,      
        [Parameter(Mandatory = $true)]
        [string]$EmailSubject
        )
       
       $ErrorActionPreference = "SilentlyContinue"
        $to = "$EmailTo"
        $emailCC = "$EmailCC"
        $msg = New-Object Net.Mail.MailMessage
        $smtp = New-Object Net.Mail.SmtpClient("smtp.test.prv")
        $msg.From = "$EmailFrom"
        $msg.ReplyTo = "$ReplyTo"
        foreach ($address in $to) {
            $msg.To.Add($address)
        }
        foreach ($address in $emailCC) {
            $msg.CC.Add($address)
        }
        foreach ($address in $emailBcc) {
            $msg.Bcc.Add($address)
        }
        $msg.Subject = "$EmailSubject"
        $msg.Attachments.Add($EmailAttachment)
        $msg.Body = $EmailBody
        $smtp.Send($msg)
        $EmailAttachment.Dispose()
        $msg.Dispose()

    }

With the command looking like this, and it works with 1 email address in the To parameter;

SendEmail -EmailTo "olaf@powershellforums.org" -EmailFrom "Email Test<Email_Test@powershellforums.org>" -ReplyTo "me@test.com" -EmailAttachment $variable1 -EmailBody "Body Text" -EmailSubject "Subject text"

Problem is when I want to have more than 1 email address in the -EmailTo or -EmailCC fields.

SendEmail : Cannot process argument transformation on parameter 'EmailTo'. Cannot convert value to type System.String.
At line:1 char:20
+ ... il -EmailTo "user1@test.com","user2@test.com" - ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [SendEmail], ParameterBindingArgumentTransformationException
    + FullyQualifiedErrorId : ParameterArgumentTransformationError,SendEmail

Just looking for help as to what I’m doing wrong, and anything would be appreciated. Not as fluent on the “Advanced Functions” front.

If you want to pass multiple values to a parameter you have to specify an array type in your parameter definition.

Instead

you should use

        [Parameter(Mandatory = $true)]
        [string[]]$EmailTo,      

The same goes for CC or BCC.

And this

is unnecessary. Drop it completely. Use the variables directly.
The same goes for

And BTW: you never defined $emailBcc but you’re using it:

Thank you very much. Not sure how that part didn’t click with me, but again, not too deep into functions like this.
And thank you as well for the cleanup. I think I only kept that because that block of code is how I would send the emails in the past. Just wanted to make it a one-liner since I’m sending reports to several different individuals from the same script.

This doesn’t make any sense. The more you need to depend on it, the more robust it should be. Not the opposite. Parameterization, logging, error handling, etc should increase with importance/frequency, IMO.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.