2

I have this PowerShell command to add members to a Distributon Group in Exchange Online. This works correctly in PS. But I need to do this from a C# app.

$arr | foreach-object{Add-DistributionGroupMember -Identity 'Test' -Member $_}

I need to pass an array containing the members to the $arr in PS and pipe it to the foreach-object. I have done a lot of searching but all the examples show only how to do single a command. How do the PS command in C#?

Code snippet:

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

            using (PowerShell ps = PowerShell.Create())
            {
                ps.Runspace = runspace;

                string[] members = new string[] { "[email protected]", 
                                                  "[email protected]",                         
                };

                ps.Commands.AddParameter("Members").AddArgument(members); // ??
                ps.Commands.AddCommand("foreach-object"); // I'm stuck at this point

                Collection<PSObject> results = ps.Invoke();
            }
        }

Note:I can't do this via AddScript as remote scripts are blocked in our PS. Also doing something like below doesn't work as the runspace executes only the first pipeline.

foreach(string mem in members)
{
 Command command = new Command("Add-DistributionGroupMember");
 command.Parameters.Add("Identity", "test");
 command.Parameters.Add("Member", member);
 Pipeline pipeline = runspace.CreatePipeline();
 pipeline.Commands.Add(command);
 pipeline.Invoke();
}
4
  • Just a question, why do you call PowerShell in C#, and not just DirectoryEntry (in System.DirectoryServices) ? Commented Oct 13, 2011 at 14:02
  • How do you create a Distribution Group in Exchange Online without PowerShell? The only other API is the EWS Managed API which does not have any functionality to perform admin type tasks. Commented Oct 14, 2011 at 16:45
  • Creating them in your Active-Directory, then the Microsoft Online Services Directory Synchronization tool provides one-way synchronization from your local Active Directory directory service to Microsoft Online Services. Commented Oct 14, 2011 at 18:43
  • I'm not sure if your suggestion would work in my scenario. I'm trying to manage DGs from an Azure app. Commented Oct 21, 2011 at 16:37

2 Answers 2

1

Yeah the documentation has been lacking. I whipped up the below. Clearly not the best, but seemed to work on get-process ( I cannot try out with Add-DistributionGroupMember). You can probably improve upon it:

            using (PowerShell ps = PowerShell.Create())
            {
                ps.Runspace = runspace;
                var members = new[]
                                  {
                                      "[email protected]",
                                      "[email protected]",
                                  };
                var command1 = new Command("write-output");
                command1.Parameters.Add("InputObject", members);
                ps.Commands.AddCommand(command1);

                foreach (PSObject output in ps.Invoke())
                {
                    ps.Commands.Clear();
                    var command2 = new Command("Add-DistributionGroupMember");
                    ps.Commands.AddCommand(command2);
                    ps.Commands.AddParameter("Name", output);
                    ps.Commands.AddParameter("Identity", "test");
                    foreach (PSObject output2 in ps.Invoke())
                    {
                        Console.WriteLine(output2);
                    }
                }
            }
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the reply! Your code works but then I also found that the foreach loop I had posted earlier also works. I had a bad user and I didn't realize that first. Anyways both methods work.
0

To pass members to Add-DistributionGroupMember just include them in the Invoke() call:

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

            using (PowerShell ps = PowerShell.Create())
            {
                ps.Runspace = runspace;

                string[] members = new string[] { "[email protected]", "[email protected]" };

                Collection<PSObject> results = ps
                    .AddCommand("Add-DistributionGroupMember")
                    .AddParameter("Identity", "test")
                    .Invoke(members);
            }
        }

Note that in PowerShell you don't need to do a ForEach-Object, you can just pass in the whole array:

$arr | Add-DistributionGroupMember -Identity "test"

In both cases if you have a bad user, the good ones will still be added and an error will show for each of the bad ones.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.