0

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

Daniel Widdis
9,35513 gold badges50 silver badges70 bronze badges
asked Feb 2, 2022 at 21:37
1

1 Answer 1

0

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.

answered Mar 1, 2022 at 0:12
Sign up to request clarification or add additional context in comments.

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.