Search code examples
powershellexchange-serversend-on-behalf-of

Exchange 2010 - Powershell to remove Send On Behalf permission


I'm trying to run the command below to remove e Send On Behalf permission, but i'm getting an exception and it removes all the users that have access, instead of the one I specify in my script

$owner = "lpeter" 
$remove = "jdoe"

$grantlist = Get-Mailbox $owner -DomainController tordc01 | select -ExpandProperty GrantSendOnB

$grantlist = $grantlist |?{$_.Name -ne $remove} 
Set-Mailbox $owner -GrantSendOnBehalfTo $null -DomainController tordc01
$grantlist | %{
    Set-Mailbox $owner -GrantSendOnBehalfTo @{Add=$_.Name} -Confirm $true
} -DomainController tordc01

here is the exception :

ForEach-Object : Cannot bind parameter 'Process'. Cannot convert the
"-DomainController" value of type "System.String" to type
"System.Management.Automation.ScriptBlock". At line:1 char:15
+ $grantlist | % <<<< {Set-Mailbox $owner -GrantSendOnBehalfTo @{Add=$_.Name} -Confirm $true} -DomainController tordc01

     + CategoryInfo          : InvalidArgument: (:) [ForEach-Object], ParameterBindingException
     + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.ForEachObjectCommand

Solution

  • The exception is pretty self-explanatory, you're trying to supply the -DomainController parameter to ForEach-Object, instead of to Set-Mailbox

    Change the last statement to:

    $grantlist | %{
        Set-Mailbox $owner -GrantSendOnBehalfTo @{Add=$_.Name} -Confirm:$true -DomainController tordc01
    }