Search code examples
c#powershellforeachpowershell-remoting

C# Powershell Pipeline foreach-object


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();
}

Solution

  • 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);
                        }
                    }
                }