Avatar of Lee Parker
Lee Parker
Flag for United Kingdom of Great Britain and Northern Ireland asked on

Powershell script to grant send on behalf permissions from one array of mailboxes to another in exchange

First time post so if I have done anything wrong here let me know!

In office 365/exchange online I have the task of providing one list of mailboxes send on behalf permissions to each member of the 2nd list. So far I have a very simple script that looks like:

#arr1 mailboxes 
#arr2 send on behalf access permissions to arr1 mailboxes

$arr1 = 'ab', cd'
$arr2 = 'ef', 'gh'


ForEach($i in $arr1){
    ForEach($j in $arr2){
   
        Set-Mailbox -identity "$i" -GrantSendOnBehalfTo "$j"
   
    }


}

Open in new window


The problem here is that 'gh' has send on behalf permissions to 'ab' and 'gh' but 'ef' does not!

Looks like my logic is wrong as the final item in arr2 gets the send on behalf permission but the rest do not. How do I give all items in the 2nd array send on behalf permissions and not just the last one?

**update** Logic is fine. What is happening is exchange is replacing the permissions so as 'ef' is granted access to 'ab', when powershell iterates over to to 'gh' it overrides the permissions 'ef' already had, resulting in 'gh' having send on behalf permissions only...
PowershellExchange* pnppowershell

Avatar of undefined
Last Comment
Lee Parker

8/22/2022 - Mon
ste5an

1) Use speaking variable names.
2) Output what you're trying to do.

$AccountsToManage = 'ab', 'cd'
$AccountsOnBehalfSender = 'ef', 'gh'

ForEach($ToManage in $AccountsToManage){
  ForEach($OnBehalfSender in $AccountsOnBehalfSender){   
    Write-Host "$OnBehalfSender sends for $ToManage."   
    # Set-Mailbox -identity "$ToManage" -GrantSendOnBehalfTo "$OnBehalfSender"
  }
}

Open in new window


3) Rephrase your question and explain your logic a little bit better. Add a concise and complete example.
Lee Parker

ASKER
Sorry about the variable names, you're right, bad practice on my part.

The outputs show exactly what needs to be done. I now believe this is more of a problem with exchange online than the logic in the powershell script. As the script iterates through the 2nd array (arr2 or accountsOnBehalfSender) it replaces the last element with the next until the final element is the only one with the permissions, rather than adding them all... 
ASKER CERTIFIED SOLUTION
ste5an

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Lee Parker

ASKER
Thanks, Ste5an this worked! Just missing the closing bracket at the end for anyone who might use this to solve a similar problem.

$AccountsToManage = 'ab', 'cd'
$AccountsOnBehalfSender = 'ef', 'gh'

ForEach($ToManage in $AccountsToManage){
  Write-Host "Setting grants for $ToManage."   
  # Set-Mailbox -identity "$ToManage" -GrantSendOnBehalfTo @{add=$AccountsOnBehalfSender}
}

Open in new window

Your help has saved me hundreds of hours of internet surfing.
fblack61