0

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
Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206
Nazim
  • 15
  • 3
  • 9

1 Answers1

1

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
}
Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206
  • Here is th exception I get now after changing the last statement A positional parameter cannot be found that accepts argument 'True'. + CategoryInfo : InvalidArgument: (:) [Set-Mailbox], ParameterBindingException + FullyQualifiedErrorId : PositionalParameterNotFound,Set-Mailbox – Nazim Mar 10 '15 at 16:05
  • 1
    Change `-Confirm $true` to `-Confirm:$true`. Use `-Confirm:$false` if you want to avoid confirming every user added – Mathias R. Jessen Mar 10 '15 at 17:29