PowerShell ParameterBindingException

时间:2011-04-30 10:32:41

标签: c# exception powershell exchange-server

我目前正在构建一个应用程序,以便从ASP.NET MVC网站自动执行某些Exchange 2010操作。

现在,当我尝试调用New-AddressList命令时,我遇到了一个ParameterBindingException。

我正在尝试创建以下调用(有效):

new-AddressList -Name "7 AL" -RecipientContainer "myDomain.local/Customers/7" -IncludedRecipients 'AllRecipients' -Container '\' -DisplayName "7 AL"

我是通过以下方式完成的:

var NewAddressList = new Command("New-AddressList");
NewAddressList.Parameters.Add("Name", "7 AL");
NewAddressList.Parameters.Add("RecipientContainer", "myDomain.local/Customers/7");
NewAddressList.Parameters.Add("IncludedRecipients", "AllRecipients");
NewAddressList.Parameters.Add("Container", @"\");
NewAddressList.Parameters.Add("DisplayName", "7 AL");
CommandsList.Add(NewAddressList);

这个命令列表提供给我调用的管道,给出了以下错误:

New-AddressList:输入对象不能绑定到命令的任何参数,因为该命令不接受管道输入或输入及其属性与管道输入的任何参数都不匹配。

  • CategoryInfo:InvalidArgument:(7:PSObject)[New-AddressList],ParameterBindingException
  • FullyQualifiedErrorId:InputObjectNotBound,Microsoft.Exchange.Management.SystemConfigurationTasks.NewAddressList

可能导致此问题的任何线索?

使用Trace-Command输出:

PS C:\Users\ext_kefu> Trace-Command -Name parameterbinding -Expression {New-AddressList -Name "7 AL" -RecipientContainer "myDomain.local/Customers/7" -IncludedRecipients 'AllRecipients' -Container '\' -DisplayName "7 AL"} -PSHost
DEBUG: ParameterBinding Information: 0 : BIND NAMED cmd line args [New-AddressList]
DEBUG: ParameterBinding Information: 0 :     BIND arg [7 AL] to parameter [Name]
DEBUG: ParameterBinding Information: 0 :         COERCE arg to [System.String]
DEBUG: ParameterBinding Information: 0 :             Parameter and arg types the same, no coercion is needed.
DEBUG: ParameterBinding Information: 0 :         BIND arg [7 AL] to param [Name] SUCCESSFUL
DEBUG: ParameterBinding Information: 0 :     BIND arg [myDomain.local/Customers/7] to parameter [RecipientContainer]
DEBUG: ParameterBinding Information: 0 :         COERCE arg to [Microsoft.Exchange.Configuration.Tasks.OrganizationalUnitIdParameter]
DEBUG: ParameterBinding Information: 0 :             Trying to convert argument value from System.String to Microsoft.Exchange.Configuration.Tasks.OrganizationalUnitIdParameter
DEBUG: ParameterBinding Information: 0 :             CONVERT arg type to param type using LanguagePrimitives.ConvertTo
DEBUG: ParameterBinding Information: 0 :             CONVERT SUCCESSFUL using LanguagePrimitives.ConvertTo: [myDomain.local/Customers/7]
DEBUG: ParameterBinding Information: 0 :         BIND arg [myDomain.local/Customers/7] to param [RecipientContainer] SUCCESSFUL
DEBUG: ParameterBinding Information: 0 :     BIND arg [AllRecipients] to parameter [IncludedRecipients]
DEBUG: ParameterBinding Information: 0 :         COERCE arg to [System.Nullable[Microsoft.Exchange.Data.Directory.Recipient.WellKnownRecipientType]]
DEBUG: ParameterBinding Information: 0 :             Trying to convert argument value from System.String to System.Nullable[Microsoft.Exchange.Data.Directory.Recipient.WellKnownRecipientType]
DEBUG: ParameterBinding Information: 0 :             CONVERT arg type to param type using LanguagePrimitives.ConvertTo
DEBUG: ParameterBinding Information: 0 :             CONVERT SUCCESSFUL using LanguagePrimitives.ConvertTo: [AllRecipients]
DEBUG: ParameterBinding Information: 0 :         BIND arg [AllRecipients] to param [IncludedRecipients] SUCCESSFUL
DEBUG: ParameterBinding Information: 0 :     BIND arg [\] to parameter [Container]
DEBUG: ParameterBinding Information: 0 :         COERCE arg to [Microsoft.Exchange.Configuration.Tasks.AddressListIdParameter]
DEBUG: ParameterBinding Information: 0 :             Trying to convert argument value from System.String to Microsoft.Exchange.Configuration.Tasks.AddressListIdParameter
DEBUG: ParameterBinding Information: 0 :             CONVERT arg type to param type using LanguagePrimitives.ConvertTo
DEBUG: ParameterBinding Information: 0 :             CONVERT SUCCESSFUL using LanguagePrimitives.ConvertTo: [\]
DEBUG: ParameterBinding Information: 0 :         BIND arg [\] to param [Container] SUCCESSFUL
DEBUG: ParameterBinding Information: 0 :     BIND arg [7 AL] to parameter [DisplayName]
DEBUG: ParameterBinding Information: 0 :         COERCE arg to [System.String]
DEBUG: ParameterBinding Information: 0 :             Parameter and arg types the same, no coercion is needed.
DEBUG: ParameterBinding Information: 0 :         BIND arg [7 AL] to param [DisplayName] SUCCESSFUL
DEBUG: ParameterBinding Information: 0 : BIND POSITIONAL cmd line args [New-AddressList]
DEBUG: ParameterBinding Information: 0 : MANDATORY PARAMETER CHECK on cmdlet [New-AddressList]
DEBUG: ParameterBinding Information: 0 : CALLING BeginProcessing
DEBUG: ParameterBinding Information: 0 : CALLING EndProcessing

Name                      DisplayName               RecipientFilter
----                      -----------               ---------------
7 AL                      7 AL                      Alias -ne $null

2 个答案:

答案 0 :(得分:2)

我发现必须单独调用每个命令,因为它们不相关。问题源于我对Powershell管道概念的误解。

答案 1 :(得分:1)

为什么要将命令声明为var?我的意思是:

Command NewAddressList = new Command("New-AddressList");

然后,尝试将命令添加为CommandParameter个对象(如建议的here):

CommandParameter NameParam = new CommandParameter("Name","7 AL");
NewAddressList.Parameters.Add(NameParam);

最后,为什么不直接使用Powershell类?


编辑:追踪后进一步猜测

您正在使用的Parameters.Add的重载版本将键(字符串)和值(对象)作为参数。可能c#不会做同样出色的工作:/。尝试将值作为所需类型的对象传递。例如,included-recipient参数需要Microsoft.Exchange.Data.Directory.Recipient.WellKnownRecipientType

尝试演员表。

示例:

Microsoft.Exchange.Data.Directory.Recipient.WellKnownRecipientType allrecips = (Microsoft.Exchange.Data.Directory.Recipient.WellKnownRecipientType) "AllRecipients";
NewAddressList.Parameters.Add("IncludedRecipients", allrecips);

或(我知道这可能很愚蠢):

NewAddressList.Parameters.Add("IncludedRecipients", "[Microsoft.Exchange.Data.Directory.Recipient.WellKnownRecipientType] AllRecipients");