英文:
C# using Powershell.AddParameter with two conditions
问题
我想在C#中调用PowerShell脚本。在终端中,命令如下:
get-mailbox -RecipientTypeDetails "EquipmentMailbox","RoomMailbox"
如果我只使用一个收件人类型,例如"RoomMailbox",那么可以正常运行。
ps.AddCommand("Get-Mailbox");
ps.AddParameter("RecipientTypeDetails", "RoomMailbox");
但是我不知道如何将"EquipmentMailbox"添加到参数条件中,以显示两种类型。
ps.AddCommand("Get-Mailbox");
ps.AddParameter("RecipientTypeDetails", "RoomMailbox,EquipmentMailboxes");
谢谢!
英文:
I'd like to call a PowerShell script in c#. In Terminal the command would look like
get-mailbox -RecipientTypeDetails "EquipmentMailbox","RoomMailbox"
I can get it to run if I just use one Recipient type, for example "RoomMailbox", this works fine.
ps.AddCommand("Get-Mailbox");
ps.AddParameter("RecipientTypeDetails" , "RoomMailbox");
but i dont know how to add EquipmentMailbox to the parameter conditions, so to show both types.
ps.AddCommand("Get-Mailbox");
ps.AddParameter("RecipientTypeDetails" , "RoomMailbox,EquipmentMailboxes");
Thanks!
答案1
得分: 3
你需要创建一个数组,将两个参数绑定到RecipientTypeDetails
参数。测试参数如何绑定的简单方法是定义一个简单的PowerShell脚本:
public static Collection<PSObject> Run()
{
using PowerShell ps = PowerShell.Create()
.AddScript(@"
[cmdletbinding()]
param($RecipientTypeDetails)
$PSBoundParameters
")
.AddParameter(
"RecipientTypeDetails",
new string[] { "RoomMailbox", "EquipmentMailbox" });
return ps.Invoke();
}
或者,如果cmdlet有许多参数,您可以使用AddParameters
更容易:
public static Collection<PSObject> Run()
{
using PowerShell ps = PowerShell.Create()
.AddScript(@"
[cmdletbinding()]
param($foo, $bar, $baz)
$PSBoundParameters
")
.AddParameters(new Dictionary<string, object>()
{
{ "foo", new string[] { "RoomMailbox", "EquipmentMailbox" } },
{ "bar", "hello" },
{ "baz", "world" }
});
return ps.Invoke();
}
英文:
You need to create an array to bind both arguments to the RecipientTypeDetails
parameter. A simple way to test how arguments are bound is to define a simple PowerShell script:
public static Collection<PSObject> Run()
{
using PowerShell ps = PowerShell.Create()
.AddScript(@"
[cmdletbinding()]
param($RecipientTypeDetails)
$PSBoundParameters
")
.AddParameter(
"RecipientTypeDetails",
new string[] { "RoomMailbox", "EquipmentMailbox" });
return ps.Invoke();
}
Alternatively, if the cmdlet has many parameters, you might find easier by using AddParameters
instead:
public static Collection<PSObject> Run()
{
using PowerShell ps = PowerShell.Create()
.AddScript(@"
[cmdletbinding()]
param($foo, $bar, $baz)
$PSBoundParameters
")
.AddParameters(new Dictionary<string, object>()
{
{ "foo", new string[] { "RoomMailbox", "EquipmentMailbox" } },
{ "bar", "hello" },
{ "baz", "world" }
});
return ps.Invoke();
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论