0

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();
matth
  • 6,112
  • 4
  • 37
  • 43
Murda Ralph
  • 175
  • 2
  • 4
  • 16
  • I have been able to successfully run this code when inputting each email address by themselves. – Murda Ralph Oct 23 '13 at 21:06
  • Although the parameter states i can insert multiple values -parameter value1,value2,etc it looks like its seeing it as one entire address making it invalid. – Murda Ralph Oct 23 '13 at 21:24

2 Answers2

1

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);
Murda Ralph
  • 175
  • 2
  • 4
  • 16
0

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.

Adi Inbar
  • 12,097
  • 13
  • 56
  • 69
  • i tried your code. Someone had previously suggested the same code but didnt work so they removed their answer. I had to go with an array – Murda Ralph Oct 26 '13 at 00:05
  • That was my main point, that you need to pass an array. The code I posted was just an alternative suggestion as to how one might pass an array to PowerShell as a string, because if you provide a comma-separated list of quoted strings at a PowerShell command line, it's interpreted as an array argument. The key word was "*if* it's passed to PowerShell as-is". – Adi Inbar Oct 26 '13 at 00:31