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" }}
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
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" }}
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...
2) Output what you're trying to do.
Open in new window
3) Rephrase your question and explain your logic a little bit better. Add a concise and complete example.