Powershell 多个 AddParameter C# [英] Powershell multiple AddParameter C#
问题描述
我从 C# 表单应用程序中运行 PowerShell.我想向我的 PowerShell 命令添加多个参数.
I run a PowerShell from within a C# form application. I want to add multiple parameters to my PowerShell command.
但是当我输入第二个 AddParameter
调用时,它失败并显示错误:
But when I enter a second AddParameter
call, it failed with the error:
System.Management.Automation.ParameterBindingException
System.Management.Automation.ParameterBindingException
代码:
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.
看起来它只接受 1 个参数.
It looks like it only accepts 1 parameter.
还有一个 AddParameters
,但我无法使其正常工作.
There is also a AddParameters
, but I'm not able to get this working.
有人有这方面的经验吗?
Anyone got any experience with this?
推荐答案
实际上不能将 name
和 InterfaceIndex
与 Get 同时使用-NetAdapter
cmdlet.这是因为它们属于不同的参数集,因此必须单独使用.
You can't actually use name
and InterfaceIndex
at the same time with the Get-NetAdapter
cmdlet. This is because they belong to different Parameter Sets and as a result must be used separately.
要查看该 cmdlet 的参数集的详细信息,请参见此处:https://technet.microsoft.com/en-us/library/jj130867(v=wps.630).aspx
To see details of the Parameter Sets for that cmdlet see here: https://technet.microsoft.com/en-us/library/jj130867(v=wps.630).aspx
或者在 PowerShell 中使用 Get-Help Get-NetAdapter
.
Or use Get-Help Get-NetAdapter
in PowerShell.
但是,如果您在使用同一参数集中允许的参数添加参数时仍然遇到问题,您可以尝试将两个参数添加为单个命令,如下所示(使用 name
和 throttlelimit
为例,因为它们在同一参数集中被允许:ByName
for 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
):
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)
来源:https://msdn.microsoft.com/en-us/library/dn614674(v=vs.85).aspx
这篇关于Powershell 多个 AddParameter C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!