C# Powershell 管道 foreach-object
我使用此 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,缺少文档。我鞭打了下面的内容。显然不是最好的,但似乎适用于
get-process
(我无法尝试使用Add-DistributionGroupMember
)。你也许可以改进它: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 withAdd-DistributionGroupMember
). You can probably improve upon it:要将成员传递给 Add-DistributionGroupMember,只需将它们包含在 Invoke() 调用中:
请注意,在 PowerShell 中,您不需要执行 ForEach-Object,只需传入整个数组即可:
在这两种情况下,如果您有一个坏的数组对于用户来说,好的内容仍然会被添加,并且每个坏的内容都会显示一个错误。
To pass members to Add-DistributionGroupMember just include them in the Invoke() call:
Note that in PowerShell you don't need to do a ForEach-Object, you can just pass in the whole array:
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.