locked
Powershell for Exchange 2007 Enable-Mailbox Script RRS feed

  • Question

  • Hey Everyone,

    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
    

    Wednesday, March 9, 2011 6:05 PM

Answers

  • This should work:

    $RData = ((Get-Mailbox -Identity $RUser).Database).DistinguishedName

    Karl


    http://unlockpowershell.wordpress.com
    -join("6B61726C6D69747363686B65406D742E6E6574"-split"(?<=\G.{2})",19|%{[char][int]"0x$_"})
    • Marked as answer by paulkatiem Wednesday, March 9, 2011 8:09 PM
    Wednesday, March 9, 2011 6:34 PM
  • Try

    $REmail = ((Get-Mailbox -Identity "$REmailInput").PrimarySmtpAddress).ToString()

    Karl


    http://unlockpowershell.wordpress.com
    -join("6B61726C6D69747363686B65406D742E6E6574"-split"(?<=\G.{2})",19|%{[char][int]"0x$_"})
    • Marked as answer by paulkatiem Thursday, March 10, 2011 6:40 PM
    Thursday, March 10, 2011 4:51 PM

All replies

  • This should work:

    $RData = ((Get-Mailbox -Identity $RUser).Database).DistinguishedName

    Karl


    http://unlockpowershell.wordpress.com
    -join("6B61726C6D69747363686B65406D742E6E6574"-split"(?<=\G.{2})",19|%{[char][int]"0x$_"})
    • Marked as answer by paulkatiem Wednesday, March 9, 2011 8:09 PM
    Wednesday, March 9, 2011 6:34 PM
  • Thanks tons Karl!! That did the trick, you da man!
    Wednesday, March 9, 2011 8:10 PM
  • Anyone want to see my Script when it's finished?
    Wednesday, March 9, 2011 8:10 PM
  • You are welcome :)

    Karl


    http://unlockpowershell.wordpress.com
    -join("6B61726C6D69747363686B65406D742E6E6574"-split"(?<=\G.{2})",19|%{[char][int]"0x$_"})
    Wednesday, March 9, 2011 8:29 PM
  • Anyone want to see my Script when it's finished?


    ABSOLUTLEY!

    Karl


    http://unlockpowershell.wordpress.com
    -join("6B61726C6D69747363686B65406D742E6E6574"-split"(?<=\G.{2})",19|%{[char][int]"0x$_"})
    Wednesday, March 9, 2011 8:30 PM
  • Write-output “ENTER THE FOLLOWING DETAILS”
    $DName = Read-Host “User Diplay Name"
    $RUser = Read-Host "Replicate User(Database Grab)"
    $RData = ((Get-Mailbox -Identity $RUser).Database).DistinguishedName
    $REmail = Read-Host “Requester's Email Address"
    
    Enable-Mailbox -Identity "$DName" -Database "$RData" 
    Set-CASMailbox -Identity "$DName" -ActiveSyncEnabled $false -ImapEnabled $false -PopEnabled $false
    
    
    Send-MailMessage -From "John Doe <John.Doe@Generic.com>" -To $REmail -Subject "$DName's email account" -Body "$DName's email account has been setup.`n`n`nJohn Doe`nGeneric Co.`nSystems Administrator`nOffice: 123.456.7890`nJohn.Doe@Generic.com" -SmtpServer GenericCo.Generic.com

    Here it is! My very last/complete revision will just prompt for Requester's name, not email address and pull it with another command...

    But this was all I had time for today and am super pumped to have it working, will save TONS of time!

    • Marked as answer by paulkatiem Thursday, March 10, 2011 6:43 PM
    • Unmarked as answer by paulkatiem Thursday, March 10, 2011 6:43 PM
    Wednesday, March 9, 2011 8:59 PM
  • But this was all I had time for today and am super pumped to have it working, will save TONS of time!

    Feels GREAT, doesn't it?

    Karl


    http://unlockpowershell.wordpress.com
    -join("6B61726C6D69747363686B65406D742E6E6574"-split"(?<=\G.{2})",19|%{[char][int]"0x$_"})
    Wednesday, March 9, 2011 9:03 PM
  • Yeah it really does. I really do appreciate your help! I've been doing maintenance and basic things with powershell, but it feels good to start getting my hands dirty with the scripting side of it.

    I tried using this to grab the Requester's primary email address, but it doesn't seem to like it... Maybe it's not being handed off properly formatted?

    $REmailInput = Read-Host “Requester's Name(Notification Email goes to this Person)"
    $REmail = Get-Mailbox -Identity "$REmailInput" | Select-Object PrimarySmtpAddress

    Thursday, March 10, 2011 4:10 PM
  • Try

    $REmail = ((Get-Mailbox -Identity "$REmailInput").PrimarySmtpAddress).ToString()

    Karl


    http://unlockpowershell.wordpress.com
    -join("6B61726C6D69747363686B65406D742E6E6574"-split"(?<=\G.{2})",19|%{[char][int]"0x$_"})
    • Marked as answer by paulkatiem Thursday, March 10, 2011 6:40 PM
    Thursday, March 10, 2011 4:51 PM
  • Awesome, worked exactly as expected! Is there a way that I can have information returned on screen to make the email part of this verbose?
    Thursday, March 10, 2011 6:39 PM
  • Do you mean something like "We will send mail to bob@contoso.com"?

    if so, try:

    Write-Host "We will send email to $REmail"

    Karl


    http://unlockpowershell.wordpress.com
    -join("6B61726C6D69747363686B65406D742E6E6574"-split"(?<=\G.{2})",19|%{[char][int]"0x$_"})
    Thursday, March 10, 2011 6:51 PM
  • It's not a big deal either way, but it was more for me to see if the email was successful...

     

    I say it's not a big deal, because when I close out the work ticket it notifies the requester anyways. I was asking you more to learn not only for this but for future scripts that I build to see right away if they work or not.

    I can't say thank you enough for the help you've given me. These scripts are great platforms for me to build/learn on!

    Thursday, March 10, 2011 7:24 PM