Automating mailbox location tracing for Microsoft Exchange mixed mode
“Someone” deleted a user account from your AD environment and now you’ve been asked to restore it. You will have no idea where it was residing and even if you found that out, where the heck was the mailbox? Now imagine if that account belonged to the VP of the company… Oh boy!!!
So, this came to me as a requirement and I looked up over internet and couldn’t find any. So decided to write one on my own. I will try to put this in layman terms to ensure both Mr.Perfect and Mr.JustOk can follow it.
Scenario:
If an exchange mailbox for an account is deleted, exchange will mark it as disconnected mailbox and it will be deleted post the retention period set in your organization. To reconnect that mailbox, it is mandatory to know where it was residing so that you could navigate to that database and reconnect it to an AD account. Exchange does not possess the functionality to remember or audit the mailbox location of a user by default.
Solution:
This is a script which will collect details of all user accounts in the following order:
Name | Alias | PrimarySmtpAddress | Database | Identity |
So here we go:
-
Have a machine dedicated for this purpose. I will call it ExchSrvr.MSExchangeGuru.com. It can be a mailbox server, but remember the output of this script will depend upon how big your organization is and this will create an output every week/how frequent you want to schedule it.
To give you a rough idea: The output for an organization with about 30000 users was 4.5MB when I ran it. So, do you math’s and find out the output file size.
- Create a folder à C:\Scripts\MailboxReport
- Run “Set-ExecutionPolicy RemoteSigned” cmdlet in shell since this is a custom script and it won’t run if it’s not signed.
- Copy this script and paste it to “C:\Scripts\MailboxReport\” with name “_MailboxReport_Script.ps1”
# _MailboxReport_Script.ps1 – Get the report of all user mailboxes and their server location and database. # Created by – www.MSExchangeGuru.com$AdminSessionADSettings.ViewEntireForest =$True #Adding Exchange Snap In to execute Exchange CmdLets in this script Add-PSSnapin Microsoft.Exchange.Management.PowerShell.Admin #Get UserBase report to CSV file $strB = Get-Date $strDt = “C:\Scripts\MailboxReport\MailboxReport_” + $strB.Year + $strB.Month + $strB.Day + “_” +$strB.Hour + $strB.Minute + “.CSV” Get-Mailbox -ResultSize Unlimited | Select Name, Alias, PrimarySmtpAddress, Database, Identity| Export-CSV $strDt #Send an email $FromAddress = “MailboxReport@MSExchangeGuru.com” $ToAddress = “Meera@MSExchangeGuru.com, Sai@MSExchangeGuru.com, Sunder@MSExchangeGuru.com, $MessageSubject = “Mailbox Report” $MessageBody = “Hi Team, Attached is the MSExchangeGuru Mailbox Report for this week. – Ratish Nair” $SendingServer = “Type the FQDN of your HubTransport server or your Gateway server which can send emails” $SMTPMessage = New-Object System.Net.Mail.MailMessage $FromAddress, $ToAddress, $MessageSubject, $MessageBody $Attachment = New-Object Net.Mail.Attachment($strDt) $SMTPMessage.Attachments.Add($Attachment) $SMTPClient = New-Object System.Net.Mail.SMTPClient $SendingServer $SMTPClient.Send($SMTPMessage) |
Let me tell you what this script means:
$AdminSessionADSettings.ViewEntireForest =$True – allows you to open a session for the entire forest, so if you have multiple domains, you don’t have to run this script for each domain.
$strDt = “C:\Scripts\MailboxReport\MailboxReport_” + $strB.Year + $strB.Month + $strB.Day + “_” +$strB.Hour + $strB.Minute + “.CSV” – This is where we specify the output file name. So if I run the script at 10.15am on 5/20/11 the output will be named as MailboxReport_2011520_1015.csv
Now we run the cmdlet specified in the script to get the output.
Once the output is created, we send an email from “any” email address to a set of recipients.
I have marked some fields like this. You need to edit these areas to suit your organization requirements.
- Once the _MailboxReport_Script.ps1 is created, try running it once to ensure you are getting the required output along with the automated email.
- If you don’t want the script to parse the entire directory while testing, just remove “ResultSize Unlimited” from the line “Get-Mailbox -ResultSize Unlimited | Select Name, Alias, PrimarySmtpAddress, Database, Identity| Export-CSV $strDt” and replace it with the alias of a user.
- Once the script is doing “what it is supposed to be doing”, just schedule it using Windows scheduler to run every week.
NOTE– The output of this script is vital for any/every organization. It is important to have a backup of its output every week. This can be used for Auditing, DR and administrative purposes. If you don’t have a similar thing running, go ahead and set it up right away.
Thanks,
Ratish Nair
MVP Exchange
Team@ MSExchangeGuru
Keywords: find list of all users in exchange, find list of all mailboxes in exchange, tracing mailbox location, script to find mailbox location in exchange 2007, script to send emails weekly, script to find all users, find all users mailbox database location, script to find all users in exchange 2007, automating scripts, automating a command
May 20th, 2011 at 11:23 am
Thanks…It was informative to me…!
May 20th, 2011 at 3:17 pm
Awesome work!! U simply made this so dam easy… it was indeed informative.. 🙂
May 20th, 2011 at 3:31 pm
Hi Ratish,
I would like to know if there is any script where I can find all mailbox users who are not having legacy exchange dn attributes
set for the entire mailboxes. Not individually database by the whole forest.
We faced a situation where some migrated users did not had legacyexchanedn value set which resulted in not processing mail submission service ending in no OOF reply for users in the same database.
May 20th, 2011 at 4:46 pm
@Vishwanath – Try something like this:
Get-Mailbox -ResultSize Unlimited | Select Name, Alias, LegacyExchangeDN >>C:\Output.txt
The output will give you all users with LegacyExchangeDN, then play with excel. Im sure there is a better way to do this though…
June 20th, 2011 at 10:54 am
Hi,
Great Script and write up.
Please help me in adding Mailbox Size in this CSV.
June 24th, 2011 at 12:43 pm
Great script bro. Good job and congrats once again on being MVP.