每个对象的 C# Powershell 管道

标签 c# powershell foreach powershell-remoting

我有这个 PowerShell 命令可以将成员添加到 Exchange Online 中的分发组。这在 PS 中正常工作。但我需要从 C# 应用程序执行此操作。

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

我需要将包含成员的数组传递给 PS 中的 $arr,并将其通过管道传递给 foreach 对象。我做了很多搜索,但所有示例仅显示了如何执行单个命令。 C#中的PS命令如何实现?

代码片段:

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

注意:我无法通过 AddScript 执行此操作,因为远程脚本在我们的 PS 中被阻止。此外,像下面这样的操作也不起作用,因为运行空间仅执行第一个管道。

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

最佳答案

是的,缺少文档。我掀起了下面。显然不是最好的,但似乎适用于 get-process(我无法尝试使用 Add-DistributionGroupMember)。您可能可以改进它:

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

关于每个对象的 C# Powershell 管道,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7749221/

相关文章:

powershell - 禁止 “Try the new cross-platform PowerShell https://aka.ms/pscore6”

PHP foreach 循环只给出 1 个结果

javascript - 循环 'System.Threading.Tasks.Task<System.Collections.Generic.List' i 类型的对象时 foreach 语句中出现编译错误

javascript - 如何将 JSON 从 ASP.NET View 模型保存到 JavaScript 变量中?

c# - DataAnnotations 中 AssociationAttribute 的用途是什么?

powershell - 比较LastWriteTime和CreationTime

c# - 如何在 visual studio 之外运行 SpecFlow 测试

javascript - 如何在 <div> HERE <div/> 中循环

c++ - 在没有 const_iterator 的情况下使用 boost_foreach

c# - 如何修改我传入的对象集合的字段