C# 使用 Powershell.AddParameter 时带有两个条件。

huangapple go评论74阅读模式
英文:

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

huangapple
  • 本文由 发表于 2023年7月28日 00:23:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76781713.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定