PowerShell ParameterBindingException

11,911

Solution 1

I found that each command must be invoked separately, since they are not related. The question arose from my misunderstanding of the concept of Powershell pipelines.

Solution 2

Why are you declaring command as var? I mean:

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

Then, try adding commands as CommandParameter objects (as suggested here):

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

Finally, why are you not using the Powershell class directly?


EDIT: Further guess after tracing

The overloaded version of Parameters.Add you are using takes as arguments a key (string) and a value (object). Probably c# does not do the same good job powershell does :/. Try passing values as objects of the required type. For example, the included-recipient parameter wants a Microsoft.Exchange.Data.Directory.Recipient.WellKnownRecipientType.

Try casts.

Example:

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

or (I know it may be silly):

NewAddressList.Parameters.Add("IncludedRecipients", "[Microsoft.Exchange.Data.Directory.Recipient.WellKnownRecipientType] AllRecipients");
Share:
11,911
kfuglsang
Author by

kfuglsang

Studying Elite Msc. Software Engineering in Denmark. Independent IT-professional with focus on .NET development.@kfuglsanglinkedin

Updated on June 11, 2022

Comments

  • kfuglsang
    kfuglsang almost 2 years

    I am currently building an application to automate some Exchange 2010 operations from a ASP.NET MVC website.

    Right now, I've run into a ParameterBindingException when I try to invoke the New-AddressList command.

    I am trying to create the following call (which works):

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

    I am doing it by the following:

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

    This commandlist is provided to a pipeline which I invoke, giving me the following error:

    New-AddressList: The input object cannot be bound to any parameters for the command either because the command does not take pipeline input or the input and its properties do not match any of the parameters that take pipeline input.

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

    Any clues to what might cause this?

    Output with Trace-Command gives:

    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
    
  • kfuglsang
    kfuglsang almost 13 years
    Thank you for your response. Unfortunately, it did not change anything. The var-thing is just a habit. I have a wrapper for the Powershell class and thus I am creating a Collection<Command> which I pass into my wrapper.
  • Emiliano Poggi
    Emiliano Poggi almost 13 years
    Are you sure the command is working either from the powershell? Are you able to get a trace using Trace-Command?
  • kfuglsang
    kfuglsang almost 13 years
    I have updated the original question with the trace output. The command works correctly.
  • kfuglsang
    kfuglsang almost 13 years
    I have just tried your additions @empo, but it still didn't change anything. By including the type inside the string, I found that C# does in fact type cast correctly. It gave me error messages that I was providing invalid values for the enum. I did find another interesting thing though. Right before my New-AddressList command, I call New-MailBoxDatabase. If I only call New-AddressList then it works (and the next command fails). So it appears to be that I might have to pause between commands or something? Perhaps some "Refresh"-thing. Forgive me for being completely new to Powershell.