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


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 {
    param (
        [Parameter(Mandatory = $true)]
        [Parameter(Mandatory = $true)]
        [Parameter(Mandatory = $false)]
        [Parameter(Mandatory = $false)]
        [Parameter(Mandatory = $false)]
        [Parameter(Mandatory = $True)]
        [Parameter(Mandatory = $true)]
       $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) {
        foreach ($address in $emailCC) {
        foreach ($address in $emailBcc) {
        $msg.Subject = "$EmailSubject"
        $msg.Body = $EmailBody


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

SendEmail -EmailTo "" -EmailFrom "Email Test<>" -ReplyTo "" -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 "","" - ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + 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.


you should use

        [Parameter(Mandatory = $true)]

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.