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[] { "testuser1@somecompany.com", 
                                                  "testuser2@somecompany.com",                         
                };

                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();
}
preambleMe
  • 33
  • 1
  • 4
  • Just a question, why do you call PowerShell in C#, and not just DirectoryEntry (in System.DirectoryServices) ? – JPBlanc Oct 13 '11 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. – preambleMe Oct 14 '11 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. – JPBlanc Oct 14 '11 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. – preambleMe Oct 21 '11 at 16:37

2 Answers2

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[]
                                  {
                                      "testuser1@somecompany.com",
                                      "testuser2@somecompany.com",
                                  };
                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);
                    }
                }
            }
manojlds
  • 290,304
  • 63
  • 469
  • 417
  • 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. – preambleMe Oct 14 '11 at 16:48
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[] { "testuser1@somecompany.com", "testuser2@somecompany.com" };

                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.

Michael Tucker
  • 205
  • 2
  • 10