1

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 ','
1
  • A lot if not all of the Exchange context provided makes no difference; this is a simple string manipulation question (in powershell). Commented Dec 30, 2014 at 16:42

2 Answers 2

3

Unfortunately just setting the separator will only set the quotes between elements. It's going to miss the first and last quote.

$target = 'Bob','Ben'

$target -replace '^|$','"' -join ','

"Bob","Ben"
Sign up to request clarification or add additional context in comments.

Comments

2
function test-Array($mailbox){
     $target= Get-Content .\list\$list.txt
     Foreach($User in $Target){
          Set-Mailbox -Identity $mailbox -GrantSendOnBehalfTo @{Add=$User}
     }
}

3 Comments

the trouble with doing in that way is that it overwrites the permissions from the last application of the Set-Mailbox command, so effectively only the last entry in the array gets the permission
What version of Exchange and Powershell are you running? I just tried using Powershell v4 and Exchange 2013 and as I expected it works flawlessly.
Exchange 2010 and im unsure of the powershell version il check it in the morning and update here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.