Due to layer 8 issues, I am required to set mailbox delegation permissions in Exchange. I have written my first functional PowerShell script to do this for me and all is well, aside from the dreaded
-GrantSendOnBehalfTo
I was confused for a while until I realized that only the last name in my array was getting the permission, and so I found that when you run the command it wipes out any existing entries.
Let's say Bob has send on behalf for Joe's mailbox and Ben also needs it. I run
Set-Mailbox -Identity Joe -GrantSendOnBehalfTo Ben
Ben will get it and Bob will lose it.
So I dug and found that you can supply multiple users in one command and this works, formats like this:
Set-Mailbox -Identity Joe -GrantSendOnBehalfTo @{Add="Bob", "Ben"}
So I want to pass my array built from some lists:
function test-Array($mailbox) {
$target= Get-Content .\list\$list.txt
Set-Mailbox -Identity $mailbox -GrantSendOnBehalfTo @{Add=$target}
}
This will obviously fail because the output of $target is:
Bob Ben
So my question is: How can I append the strings so that they are the following?
"Bob", "Ben",
I have tried
$separator = "", ""
[string]::Join($separator,$target)
But due to PowerShell being evil, it does not see " as part of my separator.
Sorry to seem the fool. I have looked around and can't for the life of me figure it out.
Resolution:
$separator = '", "'
$tgt= [string]::Join($separator,$target)
$tgt = $tgt + '"'
$tgt = $tgt.Insert(0,'"')
OR
$separator = '", "'
$target= $target -replace '^|$','"' -join ','