简体   繁体   English

Powershell多个AddParameter C#

[英]Powershell multiple AddParameter C#

I run a PowerShell from within a C# form application. 我在C#表单应用程序中运行PowerShell。 I want to add multiple parameters to my PowerShell command. 我想在PowerShell命令中添加多个参数。

But when I enter a second AddParameter call, it failed with the error: 但是当我输入第二个AddParameter调用时,它失败并出现错误:

System.Management.Automation.ParameterBindingException System.Management.Automation.ParameterBindingException

Code: 码:

PowerShell shell = PowerShell.Create().AddCommand("Get-NetAdapter");
shell.AddParameter("name", "Ethernet*");
shell.AddParameter("InterfaceIndex", "5");

foreach (PSObject result in shell.Invoke())
{
    Console.WriteLine("{0,-24}{1}", result.Members["Name"].Value,
                                    result.Members["InterfaceDescription"].Value);
} // End foreach.

It looks like it only accepts 1 parameter. 看起来它只接受1个参数。

There is also a AddParameters , but I'm not able to get this working. 还有一个AddParameters ,但我无法AddParameters工作。

Anyone got any experience with this? 有人有这方面的经验吗?

You can't actually use name and InterfaceIndex at the same time with the Get-NetAdapter cmdlet. 实际上,您无法使用Get-NetAdapter cmdlet同时使用nameInterfaceIndex This is because they belong to different Parameter Sets and as a result must be used separately. 这是因为它们属于不同的参数集,因此必须单独使用。

To see details of the Parameter Sets for that cmdlet see here: https://technet.microsoft.com/en-us/library/jj130867(v=wps.630).aspx 要查看该cmdlet的参数集的详细信息,请参阅此处: https//technet.microsoft.com/en-us/library/jj130867(v = wps.630).aspx

Or use Get-Help Get-NetAdapter in PowerShell. 或者在PowerShell中使用Get-Help Get-NetAdapter

However if you are still having issues adding parameters when using ones that are permitted in the same parameter set, you could try adding both parameters as a single command, like this (using name and throttlelimit as an example, as they are permitted in the same Parameter Set: ByName for Get-NetAdapter ): 但是,如果在使用同一参数集中允许的参数时仍然无法添加参数,则可以尝试将这两个参数作为单个命令添加,例如使用namethrottlelimit作为示例,因为它们在同一参数集中是允许的参数设置: ByNameGet-NetAdapter ):

PowerShell shell = PowerShell.Create().AddCommand("Get-NetAdapter")
                                      .AddParameter("name", "Ethernet*")
                                      .AddParameter("ThrottleLimit", 5);

Or alternatively you can use a dictionary of parameter names and values: 或者,您可以使用参数名称和值的字典:

IDictionary parameters = new Dictionary<String, String>();
parameters.Add("name", "Ethernet*");
parameters.Add("ThrottleLimit", 5);

PowerShell shell = PowerShell.Create().AddCommand("Get-NetAdapter")
   .AddParameters(parameters)

Source: https://msdn.microsoft.com/en-us/library/dn614674(v=vs.85).aspx 来源: https//msdn.microsoft.com/en-us/library/dn614674(v = vs。85).aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM