Cmdlet fails when using pipeline

Welcome Forums General PowerShell Q&A Cmdlet fails when using pipeline

This topic contains 1 reply, has 2 voices, and was last updated by

2 years, 7 months ago.

  • Author
  • #61056

    Topics: 1
    Replies: 0
    Points: 0
    Rank: Member

    I'm making a module for my company, where we doing common task with PowerShell. One of the things is to set mailbox permissions and AD permissions on a shared mailbox with just one cmdlet.

    My cmdlet is called


    and takes the 3 parameters $Identity, $User and $AutoMapping and accepts pipeline input on $Identity

    If I call the cmdlet as follows:

    Add-NxMailboxPermission -Identity (Get-Mailbox -Identity ) -User 

    it works fine.

    If I use pipeline as follows:

    Get-Mailbox -Identity  | Add-NxMailboxPermission -User 

    only the


    part of the cmdlet is executed. No error is generated, but no Mailbox Permission is set.

    I think that maybe its param $Identity that I defined as [string[]] but Add-MailboxPermission accepts mailboxidparameter as its Identity parameter.

    My cmdlet look likes this:

    function Add-NxMailboxPermission {
        [CmdletBinding(SupportsShouldProcess, ConfirmImpact='Medium')]
           [Parameter(Mandatory, ValueFromPipelineByPropertyName)]
           [string[]] $Identity,
           [string]   $User,
           [switch]   $AutoMapping = $true
        process {
            foreach ($i in $identity) {
                if ($PSCmdlet.ShouldProcess("$i for $user", "Setting mailbox permission ('FullAccess')")) {
                    try {
                        Write-Verbose "Setting mailbox permission on $i for $user"
                        Add-MailboxPermission -Identity $i -User $User -AccessRights FullAccess -AutoMapping:$AutoMapping -Confirm:$false -ErrorAction Stop
                        Write-Verbose "Setting ADPermission on $i for $user"
                        Add-ADPermission -Identity $i -User $User -Extendedrights "Send As" -Confirm:$false -ErrorAction Stop
                    } catch {
                        Write-Warning "Can not set permission on $i for $user"

    If I change line 5 to:

    [Microsoft.Exchange.Configuration.Tasks.MailboxIdParameter[]] $Identity

    As the same type as


    return, then I get this error:

    Unable to find type [Microsoft.Exchange.Configuration.Tasks.MailboxIdParameter].
    At line:8 char:14
    + ...             [Microsoft.Exchange.Configuration.Tasks.MailboxIdParamete ...
    +                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidOperation: (Microsoft.Excha...lboxIdParameter:TypeName) [], RuntimeException
        + FullyQualifiedErrorId : TypeNotFound

    Can anyone telle me what is wrong?

    Best regards


  • #61059

    Topics: 1
    Replies: 1472
    Points: 2,324
    Helping Hand
    Rank: Community Hero

    When you specify "ValueFromPipelineByPropertyName" the output property provided by the cmdlet should have the same name like your input parameter. I would recommend to specify an alias like "sAMAccountName" or "Name".

    BTW: What's the difference between Identity and User? I thought that's pretty much the same?

The topic ‘Cmdlet fails when using pipeline’ is closed to new replies.