atmorell
atmorell

Reputation: 3068

Powershell won't use parameter value from a variable

The following command works perfectly:

Set-Mailbox $mailbox -EmailAddresses SMTP:[email protected],[email protected],[email protected]

Trying to run the same command with the email addresses in a variable crashes.

$SMTPAddresses = "SMTP:[email protected],[email protected],[email protected]” 
Set-Mailbox $mailbox -EmailAddresses $SMTPAddress

Error:

Set-Mailbox : Cannot convert 'SMTP:[email protected],[email protected],[email protected]' to the type 'Microsoft.Exchange.Data.ProxyAddressCollection' required by parameter 'EmailAddresses'. The address 'SMTP:[email protected],[email protected],[email protected]' is invalid: The address '[email protected],[email protected],[email protected]' is not a valid SMTP address.
At line:1 char:39
+  Set-Mailbox $mailbox -EmailAddresses $SMTPAddresses
+                                       ~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Set-Mailbox], ParameterBindingException
    + FullyQualifiedErrorId : CannotConvertArgument,Microsoft.Exchange.Management.RecipientTasks.SetMailbox

The variable is a string btw.

 $SMTPAddresses.GetType()

IsPublic IsSerial Name                                     BaseType                                                                                                                            
-------- -------- ----                                     --------                                                                                                                            
True     True     String                                   System.Object 

Any ideas what is causing this?

Upvotes: 0

Views: 3244

Answers (3)

Chris Rudd
Chris Rudd

Reputation: 809

A peculiarity on Office 365 powershell using the xxx-UnifiedGroups command

If you're constructing the command like this:

$emails  = @()
$emails += "SMTP:[email protected]"
$emails += "smtp:[email protected]"

# I'm splatting here but you can use whatever approach you prefer
$params  = @{}
$params += @{"EmailAddresses" = $emails}

And you're getting this error, try making sure you include a tenant address like so:

$emails  = @()
$emails += "SMTP:[email protected]"
$emails += "smtp:[email protected]"
$emails += "smtp:[email protected]"

$params  = @{}
$params += @{"EmailAddresses" = $emails}

Note: Originally I got past the error using escaping, but that was a red herring. After getting full end to end working example it turns out you need a "MOERA" address (one that matches your 365 tenant) but the command gives a false error message about not being able to convert to the type 'Microsoft.Exchange.Data.ProxyAddressCollection'.

Upvotes: 0

mjolinor
mjolinor

Reputation: 68341

This is an array of two email address strings:

$SMTPAddresses = "SMTP:[email protected]","[email protected],[email protected]

This is one string of two email addresses joined with a comma:

$SMTPAddresses = "SMTP:[email protected],[email protected],[email protected]

Upvotes: 1

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200503

The error message is rather self-explanatory. [email protected],[email protected],[email protected] is not an e-mail address. Try it like this:

$SMTPAddresses = 'SMTP:[email protected]','SMTP:[email protected]','SMTP:[email protected]'
Set-Mailbox $mailbox -EmailAddresses $SMTPAddresses

Upvotes: 1

Related Questions