C# Powershell 管道 foreach-object

发布于 2024-12-10 00:27:11 字数 1685 浏览 1 评论 0原文

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

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

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

代码片段:

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

注意:我无法通过 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();
}

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

揪着可爱 2024-12-17 00:27:11

是的,缺少文档。我鞭打了下面的内容。显然不是最好的,但似乎适用于 get-process (我无法尝试使用 Add-DistributionGroupMember)。你也许可以改进它:

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

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);
                    }
                }
            }
森末i 2024-12-17 00:27:11

要将成员传递给 Add-DistributionGroupMember,只需将它们包含在 Invoke() 调用中:

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

请注意,在 PowerShell 中,您不需要执行 ForEach-Object,只需传入整个数组即可:

$arr | Add-DistributionGroupMember -Identity "test"

在这两种情况下,如果您有一个坏的数组对于用户来说,好的内容仍然会被添加,并且每个坏的内容都会显示一个错误。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文