Question

Below is the code i'm using. I'm unable to add multiple addresses using the powershell Emailaddresses parameter. The code works fine if I just put in one email address, but once I add two addresses in the code below it returns exception stating invalid smtp address.

PSCredential credential = new PSCredential(username, password);
WSManConnectionInfo connectionInfo = new WSManConnectionInfo((new Uri(liveIdconnectionUri)), "http://schemas.microsoft.com/powershell/Microsoft.Exchange", credential);
connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Default;

Runspace runspace = System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace(connectionInfo);
PowerShell powershell = PowerShell.Create();
runspace.Open();
powershell.Runspace = runspace;

var secure = new SecureString();
foreach (char c in textBox5.Text)
{
    secure.AppendChar(c);
}

PSCommand command2 = new PSCommand();
command2.AddCommand("Set-Mailbox");
command2.AddParameter("Identity", "lferrigno");
command2.AddParameter("EmailAddressPolicyEnabled", 0);
command2.AddParameter("EmailAddresses", "SMTP:lferrigno@sscincorporated.com,lou.ferrigno@altegrahealth.com");

powershell.Commands = command2;
powershell.Invoke();
Was it helpful?

Solution

This is the code i ended up using since it was a collection.

     string[] smtp = { "SMTP:" + textBox6.Text, 9 + "smtp:" + textBox4.Text + "@sscincorporated.com" };
     command2.AddParameter("EmailAddresses", smtp);

OTHER TIPS

The -EmailAddresses parameter takes an array argument (technically a collection of SmtpProxyAddress objects, but that's not important, you can provide it an array and the conversion will be handled autonomatically), but it looks like you're giving it a single string containing multiple addresses. You need to either provide an array argument where each element is an address, or try this:

command2.AddParameter("EmailAddresses","'SMTP:lferrigno@sscincorporated.com','SMTP:lou.ferrigno@altegrahealth.com'");

Even though that's still a string within your C# code, if it's passed to PowerShell as-is, PowerShell will interpret it as an array.

You should also be able to leave out the SMTP: prefixes, because that's the default and will be set automatically if you don't specify a prefix.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top