PowerShell Script | Remove auto-map for single user on many mailboxes

983 views Asked by At

I am now approaching the world of scripting in PowerShell and I need your help with a little script that might be trivial.

I'm trying to make a script with the following requests:

  • Take via input various Mailbox / Shared Mailbox (in which the user is present)
  • Remove the user auto-mount (in this case it is found as "[email protected]".

The script is this for the moment:

#populate the list with the Mailbox / Shared to be removed
$mailboxList = Get-Content -Path "C:\Temp\Mailboxlist.txt"
$ConfirmPreference = 'none'

foreach($Mailbox in $mailboxlist)
{
        #perform the operation on the current $ mailbox
        Get-mailboxpermission -identity $Mailbox -User "[email protected]" | ? {$.AccessRights -like "FullAccess" -and $.IsInherited -eq $false } | remove-mailboxpermission -confirm:$false

 Add-MailboxPermission -Identity $Mailbox -User "[email protected]" -AccessRights:FullAccess -AutoMapping $false

}

I would like to know if this command could work correctly, also because every time I try it comes out this:

PS C:\Temp> C:\Temp\RemoveAutoMapping.ps1
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: (Microsoft.Excha...sentationObject:PSObject) [Remove-MailboxPermission], ParameterBindingException
    + FullyQualifiedErrorId : InputObjectNotBound,Microsoft.Exchange.Management.RecipientTasks.RemoveMailboxPermission
    + PSComputerName        : outlook.office365.com

Example Screenshot

1

There are 1 answers

1
kooleosis On

You might be able to try this out:

function Remove-Automap {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $true)]
        [string[]]$address,

        [Parameter(Mandatory = $true)]
        [string]$User
    )

    foreach ($mailbox in $address) {
        Remove-MailboxPermission `
            -Identity $mailbox `
            -AccessRights FullAccess `
            -Confirm:$false `
            -User $user

        Remove-RecipientPermission `
            -Identity $mailbox `
            -AccessRights SendAs `
            -Confirm:$false `
            -Trustee $user

        Set-Mailbox $mailbox -GrantSendOnBehalfTo @{remove = "$user" }
    } #foreach (removing)

    foreach ($mailbox in $address) {
        Add-MailboxPermission `
            -Identity $mailbox `
            -AccessRights FullAccess `
            -InheritanceType All `
            -AutoMapping:$false `
            -User $user

        Add-RecipientPermission -Identity "$mailbox" -AccessRights SendAs -Trustee "$user" -Confirm:$false
        Set-Mailbox $mailbox -GrantSendOnBehalfTo @{add = "$user" }
        
    } #foreach (adding)
} #function Remove-Automap

Since the functions is using the same parameters you can remove, and readd it in the same function fairly easily. Obviously change Access Rights and/or other permissions such as SendAs, as needed.