Tuesday, February 12, 2013

How to Properly Replace a User's Mailbox Due to Corruption


Occasionally users will manage to do something to their mailbox and the only alternative to fix it is to disconnect their current mailbox and create a new one. I’m an efficiently lazy admin that likes to automate the mundane stuff to make life easier for everyone.

This involves the following steps

·                     Grab information about the current mailbox
·                     Disconnect source mailbox and connect new mailbox
·                     Configure new mailbox with the email addresses from the old mailbox (X400, SMTP, X500, etc)
·                     Restore all mail items from the previous mailbox (New-MailboxRestoreRequest)
·                     Re-provision ActiveSync or BES devices (not scripted)

Here’s the script – Use it at your own risk
Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010

$idb="UserA"

#Get User Information
$mbx=get-mailbox $idb
$user=Get-User $idb


#Disconnect the current mailbox
Disable-Mailbox -Identity $idb -Confirm:$false
sleep 60
#Create a new mailbox
Enable-Mailbox -Identity $user -Database $mbx.Database.Name -Alias $mbx.alias -PrimarySmtpAddress $mbx.primarySMTPAddress

#Allow AD time to catch up
sleep 120

#Add LegacyExchangeDN as X500 address to prevent NDRs
$mbx.EmailAddresses+=("X500:{0}" -f $mbx.LegacyExchangeDN)
#Add SMTP Addresses from old mailbox to new mailbox - After Search is completed
Set-Mailbox -Identity $idb -EmailAddresses $mbx.EmailAddresses

#Get Disconnected Mailbox and create restore
$mbxserver=get-mailboxserver
$mbxList=$mbxserver|%{(Get-Mailboxstatistics -Server $_.name | ?{ $_.DisconnectDate -ne $null })}
$restore=$mbxList|Where-Object {$_.DisplayName -match $mbx.DisplayName}
$request=New-MailboxRestoreRequest -SourceStoreMailbox $restore.MailboxGUID -SourceDatabase $restore.Database -TargetMailbox $idb -AllowLegacyDNMismatch

$requestStatus=get-mailboxRestoreRequest $request.RequestGuid

while ($requestStatus.Status -notmatch "Completed"){
#Check every 5 minutes
       $requestStatus=get-mailboxRestoreRequest $request.RequestGuid
       Write-Output ("Checked at {0} : {1}" -f (Get-Date), $request.Status)
       Sleep 300
}
Write-Output "Restore Request Completed"


1 comment:

  1. This comment has been removed by a blog administrator.

    ReplyDelete