How to add/remove aliases from Exchange server collection

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,

October 14th, 2013 3:07am

You need to get the existing collection first make the modification (either add or remove) then use Set-Mailbox to add the modified collection to the Mailbox eg

            string PSServerName = "ps.outlook.com/PowerShell-LiveID?PSVersion=2.0";
            String AdminUserName = "user@domain.onmicrosoft.com";
            String Password = "password";

            System.Security.SecureString secureString = new System.Security.SecureString();
            foreach (char c in Password)
                secureString.AppendChar(c);
            PSCredential credential = new PSCredential(AdminUserName, secureString);
            WSManConnectionInfo connectionInfo = new WSManConnectionInfo(new Uri("https://" + PSServerName), "http://schemas.microsoft.com/powershell/Microsoft.Exchange", credential);
            connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Basic;
            connectionInfo.SkipCACheck = true;
            connectionInfo.SkipCNCheck = true;

            connectionInfo.MaximumConnectionRedirectionCount = 4;
            Runspace runspace = System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace(connectionInfo);
            runspace.Open();
            Pipeline plPileLine = runspace.CreatePipeline();
            Command gmGetMailbox = new Command("Get-Mailbox");
            gmGetMailbox.Parameters.Add("Identity", "user@domain.onmicrosoft.com");
            plPileLine.Commands.Add(gmGetMailbox);
            Collection<PSObject> RsResultsresults = plPileLine.Invoke();
            if (plPileLine.Error.Count == 1)
            {

                var error = plPileLine.Error.Read() as Collection<ErrorRecord>;
                if (error != null)
                {
                    foreach (ErrorRecord er in error)
                    {
                          Console.WriteLine(er.Exception.Message);
                    }
                }

            }
            plPileLine.Stop();
            foreach (PSObject obj in RsResultsresults)
            {
                PSObject prProxyAddress = (PSObject)obj.Properties["EmailAddresses"].Value;
                if (prProxyAddress.ImmediateBaseObject is System.Collections.ArrayList)
                {
                    ArrayList Addresses = (System.Collections.ArrayList)prProxyAddress.BaseObject;
                    Addresses.Add("smtp:newaddr@domain.onmicrosoft.com");
                    plPileLine = runspace.CreatePipeline();
                    Command smSetMailbox  = new Command("Set-Mailbox");
                    smSetMailbox.Parameters.Add("Identity", "user@domain.onmicrosoft.com");
                    smSetMailbox.Parameters.Add("EmailAddresses", Addresses);
                    plPileLine.Commands.Add(smSetMailbox);
                    RsResultsresults = plPileLine.Invoke();
                    if (plPileLine.Error.Count == 1)
                    {

                        var error = plPileLine.Error.Read() as Collection<ErrorRecord>;
                        if (error != null)
                        {
                            foreach (ErrorRecord er in error)
                            {
                                Console.WriteLine(er.Exception.Message);
                            }
                        }

                    }
                }

            }          
            plPileLine.Dispose();
            runspace.Close();
            runspace.Dispose();
            runspace = null;
            plPileLine = null;
Cheers
Glen

Free Windows Admin Tool Kit Click here and download it now
October 15th, 2013 1:22am

Glen thank you very much. Your solution works perfectly.

But for me this API completely not obvious. Maybe you can suggest where it described up to such details.

obvious
obvious
October 15th, 2013 3:20am

You need to get the existing collection first make the modification (either add or remove) then use Set-Mailbox to add the modified collection to the Mailbox eg

            string PSServerName = "ps.outlook.com/PowerShell-LiveID?PSVersion=2.0";
            String AdminUserName = "user@domain.onmicrosoft.com";
            String Password = "password";

            System.Security.SecureString secureString = new System.Security.SecureString();
            foreach (char c in Password)
                secureString.AppendChar(c);
            PSCredential credential = new PSCredential(AdminUserName, secureString);
            WSManConnectionInfo connectionInfo = new WSManConnectionInfo(new Uri("https://" + PSServerName), "http://schemas.microsoft.com/powershell/Microsoft.Exchange", credential);
            connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Basic;
            connectionInfo.SkipCACheck = true;
            connectionInfo.SkipCNCheck = true;

            connectionInfo.MaximumConnectionRedirectionCount = 4;
            Runspace runspace = System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace(connectionInfo);
            runspace.Open();
            Pipeline plPileLine = runspace.CreatePipeline();
            Command gmGetMailbox = new Command("Get-Mailbox");
            gmGetMailbox.Parameters.Add("Identity", "user@domain.onmicrosoft.com");
            plPileLine.Commands.Add(gmGetMailbox);
            Collection<PSObject> RsResultsresults = plPileLine.Invoke();
            if (plPileLine.Error.Count == 1)
            {

                var error = plPileLine.Error.Read() as Collection<ErrorRecord>;
                if (error != null)
                {
                    foreach (ErrorRecord er in error)
                    {
                          Console.WriteLine(er.Exception.Message);
                    }
                }

            }
            plPileLine.Stop();
            foreach (PSObject obj in RsResultsresults)
            {
                PSObject prProxyAddress = (PSObject)obj.Properties["EmailAddresses"].Value;
                if (prProxyAddress.ImmediateBaseObject is System.Collections.ArrayList)
                {
                    ArrayList Addresses = (System.Collections.ArrayList)prProxyAddress.BaseObject;
                    Addresses.Add("smtp:newaddr@domain.onmicrosoft.com");
                    plPileLine = runspace.CreatePipeline();
                    Command smSetMailbox  = new Command("Set-Mailbox");
                    smSetMailbox.Parameters.Add("Identity", "user@domain.onmicrosoft.com");
                    smSetMailbox.Parameters.Add("EmailAddresses", Addresses);
                    plPileLine.Commands.Add(smSetMailbox);
                    RsResultsresults = plPileLine.Invoke();
                    if (plPileLine.Error.Count == 1)
                    {

                        var error = plPileLine.Error.Read() as Collection<ErrorRecord>;
                        if (error != null)
                        {
                            foreach (ErrorRecord er in error)
                            {
                                Console.WriteLine(er.Exception.Message);
                            }
                        }

                    }
                }

            }          
            plPileLine.Dispose();
            runspace.Close();
            runspace.Dispose();
            runspace = null;
            plPileLine = null;
Cheers
Glen

Free Windows Admin Tool Kit Click here and download it now
October 15th, 2013 8:15am

Probably the SDK would be the best source http://msdn.microsoft.com/en-us/library/exchange/ff326157(v=exchg.140).aspx

Cheers
Glen

October 16th, 2013 2:14am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics