[英]C# Powershell Pipeline foreach-object
I have this PowerShell command to add members to a Distributon Group in Exchange Online. 我有此PowerShell命令可将成员添加到Exchange Online中的分发组。 This works correctly in PS.
这在PS中可以正常工作。 But I need to do this from a C# app.
但是我需要从C#应用程序执行此操作。
$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. 我需要将包含成员的数组传递给PS中的$ arr,并将其通过管道传递给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#?
如何在C#中使用PS命令?
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. 注意:我无法通过AddScript进行此操作,因为我们的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();
}
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
). 显然不是最好的,但似乎可以在
get-process
上工作(我无法使用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);
}
}
}
To pass members to Add-DistributionGroupMember just include them in the Invoke() call: 要将成员传递给Add-DistributionGroupMember,只需将它们包括在Invoke()调用中:
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: 请注意,在PowerShell中,您不需要执行ForEach-Object,只需传递整个数组即可:
$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. 在这两种情况下,如果您有坏用户,则仍然会添加好用户,并且每个坏用户都会显示错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.