Powershell – Retrieve a user’s Exchange database in powershell

exchangeexchange-2007powershellscripting

I've scoured the interwebs for a few days now off and on to find this. I am creating a powershell script for email-enabling new user's(Exchange 2007).

To give you a little background when we have a new hire, their AD account is created at our off-site helpdesk, but they don't create their email account.

I'm trying to automate the process of mail-enabling the user which involves putting them in the same database as an existing user, disable imap pop activesync, and lastly email the requester of the ticket.

I would like to just get prompted for the New User's name, User to Replicate(mailbox, storage group, database), and the person to email after it's been created.

So if someone could just help with a command to Retrieve a user's Exchange database in powershell that would be great, but if people also want to help with my hacked up script please do so as well!!!

Here is what I have so far:

Write-output “ENTER THE FOLLOWING DETAILS”
$DName = Read-Host “User Diplay Name"
$RUser = Read-Host "Replicate User(Database Grab)"
$RData = #get the Replicate user's mailbox database here
$REmail = #either just use a Read-Host “Requester's Email address" or ask for Requester's name and pipe through their email address by digging for it w/ powershell
Enable-Mailbox -Identity "$DName" -Database "$RData" 
Send-MailMessage -From "John Doe <John.Doe@Generic.com>" -To (put $REmail here which is the Requester's email) -Subject "Test Person's email account" -Body "Test Person's email account has been setup.`n`n`nJohn Doe`nGeneric Company`nSystems Administrator`nOffice: 123.456.7890`nJohn.Doe@Generic.com" -SmtpServer genericexchange.exchange.com

Best Answer

This should get you on the right track, the caveat being that you'd need the AD powershell stuff working; this requires at least one 2008R2 DC.

GetADUser -Identity "$RUser" -Properties homeMDB

On a side note, careful with your word-auto-formatted directional quotes - I don't know off the top of my head whether Powershell plays nice with those, but they certainly did a number on StackExchange's code coloring.

I don't envy you trying to do this in powershell - be careful to validate the inputs that are user enterable, and log the results of the commands.

Edit: You know what, that property from AD would probably not be formatted how exchange wants it. Try this instead (and won't need 2008R2):

$RData = (Get-User -Identity "$RUser" | Get-Mailbox | Select-Object Database)