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.
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.
This is a script which will collect details of all user accounts in the following order:
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
#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.
$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)
$SMTPClient = New-Object System.Net.Mail.SMTPClient $SendingServer
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.
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