Cmdlet fails when using pipeline

This topic contains 1 reply, has 2 voices, and was last updated by  Olaf Soyk 9 months, 3 weeks ago.

  • Author
    Posts
  • #61056

    Benny Andersen
    Participant

    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

    Add-NxMailboxPermission

    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

    Add-ADPermission

    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')]
        param(
           [Parameter(Mandatory, ValueFromPipelineByPropertyName)]
           [string[]] $Identity,
    
           [Parameter(Mandatory)]
           [string]   $User,
    
           [ValidateNotNullOrEmpty()]
           [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

    Get-MailBox

    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

    Benny

  • #61059

    Olaf Soyk
    Participant

    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?

You must be logged in to reply to this topic.