Script repository
The scripts disconnect a user mailbox and reconnect it. To execute either of the scripts, create a business rule, custom command or scheduled task configured for the User object type.
Script 1: Disconnect mailbox
The script disconnects a user mailbox. Also, it saves the mailbox GUID and the DN of the mailbox store where the mailbox is located in the specified attributes.
Parameters
$mbGUIDProperty- the name of the property that will store the mailbox GUID. The property must be of Binary type (e.g. CustomAttributeBinary1).$mbStorePathProperty- the name of the property that will store the mailbox database DN. The property must be of text type (e.g. CustomAttributeText1).
$mbGUIDProperty = "adm-CustomAttributeBinary1" # TODO: modify me
$mbStorePathProperty = "adm-CustomAttributeText1" # TODO: modify me
# Get mailbox GUID and mailbox store DN.
$exchangeGuid = $Context.TargetObject.Get("msExchMailboxGuid")
$mailboxStoreDN = $Context.TargetObject.Get("homeMDB")
# Save the GUID and DN.
$Context.TargetObject.Put($mbGUIDProperty, $exchangeGuid)
$Context.TargetObject.Put($mbStorePathProperty, $mailboxStoreDN)
$Context.TargetObject.SetInfo()
# Disconnect mailbox.
$Context.TargetObject.DeleteMailbox()Script 2: Reconnect mailbox
The script reconnects a previously disconnected mailbox. If the user doesn’t have a disconnected mailbox or the mailbox was deleted, the script creates a new mailbox for the user.
Parameters
$exchangeServer- the fully-qualified domain name or IP address of the Exchange Server.$alias- a template for an alias that will be assigned to the user after reconnecting a mailbox. You can use value references (e.g. %username%) to insert values of the user account properties.$mbGUIDProperty- the name of the property that stores the disconnected mailbox GUID.$mbStorePathProperty- the name of the property that that stores the mailbox database DN.
$exchangeServer = "exchangeserver.domain.com" # TODO: modify me
$alias = "%username%" # TODO: modify me
$mbGUIDProperty = "adm-CustomAttributeBinary1" # TODO: modify me
$mbStorePathProperty = "adm-CustomAttributeText1" # TODO: modify me
function CreateMailbox($alias)
{
# Select an Exchange mailbox store for the user.
$propertyPatternDNs = $Context.TargetObject.GetEx("adm-EffectivePropertyPatterns")
foreach ($propertyPatternDN in $propertyPatternDNs)
{
# Bind to the property pattern.
$propertyPattern = $Context.BindToObjectByDN($propertyPatternDN)
# Search an item that specifies an Exchange mailbox store.
foreach ($item in $propertyPattern.Items)
{
if ($item.PropertyName -ine "homeMDB")
{
continue
}
# Get the mailbox store.
$mailboxStorageDatabase = $item.GetNextMailboxStorageDatabase($Context.TargetObject)
# Create mailbox
$Context.TargetObject.CreateMailbox($alias, $mailboxStorageDatabase.AdsPath)
# Notify that the mailbox store has been used
$item.NotifyMailBoxStorageDataBaseIsUsed($Context.TargetObject, $mailboxStorageDatabase)
return
}
}
}
# Get preserved mailbox GUID and Exchange mailbox store DN.
try
{
$mailboxGuidByte = $Context.TargetObject.Get($mbGUIDProperty)
$mailboxStoreDN = $Context.TargetObject.Get($mbStorePathProperty)
}
catch
{
# No preserved information, create mailbox
CreateMailbox $alias
return
}
try
{
# Create a remote PowerShell session to the Exchange server.
$session = New-PSSession -connectionURI "http://$exchangeServer/powershell" -ConfigurationName Microsoft.Exchange
Import-PSSession -session $session -DisableNameChecking -AllowClobber
# Search user disconnected mailbox.
$mailboxGuid = New-Object "System.Guid" (,$mailboxGuidByte)
$disconnectMailbox = Get-MailboxStatistics -Database $mailboxStoreDN | Where {$_.DisconnectDate -ne $Null -and $_.Identity -eq $mailboxGuid}
# Disconnected mailbox not found, create new one.
if($disconnectMailbox -eq $NULL)
{
CreateMailbox $alias
return
}
# Reconnect the mailbox.
Connect-Mailbox -Identity $mailboxGuid -Database $mailboxStoreDN -User "%distinguishedName%"
}
finally
{
# Close the remote session and release resources.
Remove-PSSession -Session $session
}
Comments 0
You must be signed in to comment.