1

I have an array of Reciepients:

$reciepientList = @(
    "foo"
    "bar"
    #...
)

I want to send a Message to all of them with a function using REST:

function Send-Message{
    [CmdletBinding()]
    param (
        [parameter(Mandatory=$true)][String]$Message,
        [parameter(Mandatory=$true)][String]$Reciepient
    )
# My RESTful stuff goes here ...
Invoke-RestMethod ...
}

Sending my message fails:

Send-Message -Reciepient $reciepientList -Message "My message"

Output:

Send-Message: Cannot process argument transformation on parameter 'Reciepient'.
Cannot convert value to type System.String.

Any idea, how to tell Powershell to convert my Array to a String?

2
  • 1
    Your $reciepientList is not a Hash, but a string array. Change the parameter type casting from [string] to [string[]]. ([parameter(Mandatory=$true)][String[]]$Reciepient), or join the email addresses with a semi-colon (;), depending if your Send-Message function needs a combined string or an array of recipients. Commented Jan 15, 2021 at 12:31
  • 1
    You could also leave the function unchanged, instead expand the list and process each item separately $reciepientList | % { Send-Message -Reciepient $_ -Message "Your message" } Commented Jan 15, 2021 at 21:08

1 Answer 1

2

@Theo: Thanks for pointing me to the type casting. It has solved my issue!

[CmdletBinding()]
param (
    [parameter(Mandatory = $true)][String]$Message,
    [parameter(Mandatory = $true)][String[]]$Reciepient
)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.