Unable to add members with in a distribution list programmitically in Office 365

Hi All,

I am developing an application with visual studio, to create a new distribution list and then add members in this list. with the below code I am able to create the distribution list, but it is not adding the members in the list. Please note that I am adding the existing members (already in contact) in this list. Please have a look in the below code :

using (Runspace runspace = RunspaceFactory.CreateRunspace(connectionInfo))
            {
                runspace.Open();
                using (PowerShell powershell = PowerShell.Create())
                {

                    powershell.Runspace = runspace;
                    Command cmd = new Command("New-DistributionGroup");
                    cmd.Parameters.Add("Name", "Managers1");
                    cmd.Parameters.Add("IgnoreNamingPolicy");
                    cmd.Parameters.Add("MemberJoinRestriction", "Open");
                    
                    powershell.Commands.AddCommand(cmd);

                    cmd = new Command("Add-DistributionGroupMember");
                    cmd.Parameters.Add("Identity", "Managers1");
                    cmd.Parameters.Add("Member", "vinayshakti@yahoo.co.in");
                    cmd.Parameters.Add("BypassSecurityGroupManagerCheck");

                    powershell.Commands.AddCommand(cmd);
                    //Invoke the command and store the results in a PSObject collection
                    Collection<PSObject> results = powershell.Invoke();
                    //Iterate through the results and write the DisplayName and PrimarySMTP
                    //address for each mailbox
                    foreach (PSObject result in results)
                    {
                        //Console.WriteLine(string.Format("Name: {0}, PrimarySmtpAddress: {1}", result.Properties["DisplayName"].Value.ToString(), result.Properties["PrimarySmtpAddress"].Value.ToString()));
                    }
                }

            }

the above code is not giving me any exception however it is also not adding members with in the group.

I have also post this problem here : https://community.office365.com/en-us/f/172/t/404411

please  help me and let me know why is it not adding the member in the above created group.

Regards,

Vinay


August 21st, 2015 12:50pm

Your trying to add an external email address to a distribution list which would only be permitted if the Email Address was associate with an Mail Contact https://technet.microsoft.com/en-us/library/bb124519(v=exchg.150).aspx

Also you don't need to use Add-DistributionGroupMember in your example because New-DistributionGroup has a members parameter all you need is to specify the members you want in the list your creating. eg

                Command newDG = new Command("New-DistributionGroup");
                newDG.Parameters.Add(new CommandParameter("Name", "Test-DL"));
                newDG.Parameters.Add(new CommandParameter("Members", new String[]{"glen@domain.com","fred@domain.com"}));
                ps.Commands.AddCommand(newDG);

Cheers
Glen

Free Windows Admin Tool Kit Click here and download it now
August 23rd, 2015 11:55pm

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

Other recent topics Other recent topics