I'm trying to add collection of aliases to Exchange server. This can be done only via Powershell cmdlets. As Microsoft have wrapper under powershell and distributed call can be done only in runspace I use System.Management.Automation utilities for this. Command that adds aliases looks like this one:
Set-Mailbox -Identity john@contoso.com -EmailAddresses @{add=john@northamerica.contoso.com}
Where Set-Mailbox is a command, all other fields are parameters and @add shows that we add new element to existing collection.
As Exchange runspace is running in PSLanguageMode.NoLanguage mode than only Command can be executed but not Scripts. With this approach exception is risen:
Command addAliasCommand = new Command("Set-Mailbox -Identity john@contoso.com -EmailAddresses @{add=john@northamerica.contoso.com}", true);
Only clear Command with parameters can be executed:
Command addAliasCommand = new Command("Set-Mailbox", true);
addAliasCommand.Parameters.Add("identity", "test@test.onmicrosoft.com");
addAliasCommand.Parameters.Add("EmailAddresses", "testing.alias10@test.onmicrosoft.com, testing.alias11@test.onmicrosoft.com");
But problem with this approach that its completely rewrites collection of aliases, when I want to add/remove new ones.
The question is how to add pointer @Add that will show that these values are added to the existing collection of ProxyAddressCollection?
Full code:
System.Security.SecureString secureString = new System.Security.SecureString();
foreach (char c in Password) secureString.AppendChar(c); PSCredential credential = new PSCredential(AdminLogin, secureString); WSManConnectionInfo connectionInfo = new WSManConnectionInfo(new Uri("https://ps.outlook.com/PowerShell"), "http://schemas.microsoft.com/powershell/Microsoft.Exchange", credential); connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Basic; connectionInfo.SkipCACheck = true; connectionInfo.SkipCNCheck = true; connectionInfo.MaximumConnectionRedirectionCount = 4; IList<string> gmResults = null; using (Runspace runspace = RunspaceFactory.CreateRunspace(connectionInfo)) { runspace.Open(); using (Pipeline plPileLine = runspace.CreatePipeline()) { try { Command addAliasCommand = new Command("Set-Mailbox", true); addAliasCommand.Parameters.Add("identity", "test@test.onmicrosoft.com"); addAliasCommand.Parameters.Add("EmailAddresses", "testing.alias10@test.onmicrosoft.com, testing.alias11@test.onmicrosoft.com"); var rsResultsresults = plPileLine.Invoke(); if (!string.IsNullOrEmpty(resultObjectName)) { gmResults = rsResultsresults.Select(obj => obj.Members[resultObjectName].Value.ToString()).ToList(); } plPileLine.Stop(); } catch (Exception e) { return null; } finally { runspace.Close(); runspace.Dispose(); } } runspace.Close(); }
Thank you,