This morning I helped out a fellow scripter in the PowerShell forum at ScriptingAnswers.com. He was trying to figure out an Exchange 2007 problem. He wanted to update a property value, but keep the existing property values. This seems a like a reasonable request and one that isn’t limited to Exchange. There are plenty of objects you might work with in PowerShell where you want to keep the existing property value and add to it. My solution is specific to the Exchange problem, but I think you could use it as a model for similar problems with other objects.
ManageEngine ADManager Plus - Download Free Trial
Exclusive offer on ADManager Plus for US and UK regions. Claim now!
In Exchange you can grant SendOnBehalf privileges for a mailbox using the Set-Mailbox cmdlet and the –GrantSendOnBehalfTo parameter.
PS C:\> get-mailbox jeff | set-mailbox –grantSendOnBehalfto beth
This will add the user “beth” the appropriate privilege. The parameter requires a mailbox identity string like the user ‘s SAMAccountname or distinguishedname. The Jeff mailbox’s GrantSendOnBehalfTo property is now an ADInfo object with its own set of properties. The Set-Mailbox cmdlet also lets you specify multiple values for this parameter.
PS C:\> get-mailbox jeff | set-mailbox –grantSendOnBehalfto beth,lucky
The challenge was to keep the existing values. Here’s the solution I offered.
1: get-mailbox test.user24 | foreach {
2: $a=@()
3: #enumerate existing values
4: foreach ($granted in $_.GrantSendOnBehalfTo) {
5: $a+=$granted.distinguishedname
6: }
7: #add the new user
8: $a+="fflintstone"
9:
10: $_ | set-mailbox -grantSendonBehalfTo $a
11: }
I’m doing this for a single mailbox, but it it would work for a collection as well. The trick, if you will, is that you need to work with each mailbox individually so I pipe the mailbox to ForEach on Line 1.
Because the Set-Mailbox cmdlet has no way to append properties, I need to save the existing values for the GrantSendonBehalfTo property. To accomplish this I’ll initialize and empty array in Line 2. The property may have a collection of values or objects, so I need to enumerate each one as I do on Line 3. Remember, in this case each property value is an embedded object. I want to save the distinguishedname of each embedded object to the array (Line 5). Finally I need to add the name of the new user I want (Line 8). I can keep it simple by using the SAMAccountname. All that remains is to set the property using Set-Mailbox, using the array as the value. The cmdlet handles parsing the array accordingly. This works because the –GrantSendonBehalfTo parameter accepts arrays.
-GrantSendOnBehalfTo <MailboxOrMailUserIdParameter[]>
The GrantSendOnBehalfTo parameter specifies the distinguished name (DN)
of other mailboxes that can send messages on behalf of this mailbox.
Required? false
Position? Named
Default value Null
Accept pipeline input? False
Accept wildcard characters? false
I can tell from reading the help that the is looking for a MailboxOrMailUserIDParameter and the {} indicates it can accept an a array of them.
When I look at the mailboxes GrantSendonBehalfTo property I have the previously set values plus my new one.
I think you can use these principals and techniques with other objects. Remember that you likely have to work with objects individually using ForEach. Sure, you could likely simplify my code but this will likely take a scripted solution so you might as well make it meaningful.
If you come up with a similar solution for other types of objects or cmdlets I hope you’ll share.
1 thought on “Appending Property Values in PowerShell”
Comments are closed.