The script adds all Send As delegates of a mailbox to the Send On Behalf delegates list of the mailbox. The delegates that are present in the Send on Behalf list will remain in place. The script can be executed in a business rule, custom command or scheduled task configured for the corresponding object type (e.g. User).
PowerShell
# Get Exchange properties
$user = $Context.BindToObjectEx($Context.TargetObject.AdsPath , $True)
$mailboxParams = $user.GetMailParameters()
# Get 'Send As' delegates
$sendAs = $mailboxParams.SendAs
$newMailboxParams = New-Object "Softerra.Adaxes.Adsi.Exchange.AdmExchangeMailboxParameters"
$sendOnBehalfOf = $newMailboxParams.GrantSendOnBehalfTo
$sendOnBehalfOf.OverrideOldValues = $False
for ($i = 0; $i -lt $mailboxParams.SendAs.Count; $i++)
{
$trustee = $mailboxParams.SendAs.GetItem($i, [ref]"ADS_PROPERTY_NONE")
if ([System.String]::IsNullOrEmpty($trustee.ObjectSid) -or [Softerra.Adaxes.Utils.WellKnownSecurityPrincipalInfo]::IsWellKnown($trustee.ObjectSid))
{
continue
}
$objReference = New-Object "Softerra.Adaxes.Adsi.AdmObjectReference"
$objReference.ObjectSid = $trustee.ObjectSid
$sendOnBehalfOf.Add("ADS_PROPERTY_APPEND", $objReference)
}
if ($sendOnBehalfOf.Count -eq 0)
{
return
}
# Add delegates to 'Send On Behalf'
$newMailboxParams.GrantSendOnBehalfTo = $sendOnBehalfOf
$user.SetMailParameters($newMailboxParams, "ADM_SET_EXCHANGE_PARAMS_FLAGS_NONE")