Advertisement
mpansaldi84

Create Exchange Mailboxes via PowerShell

Oct 3rd, 2012
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #####################################################################
  2. #Title: Exchange Mailbox Creation              
  3. #Authors: Michael Ansaldi  
  4. #Date: 08/15/2012
  5. #                              
  6. #1)Make sure you have Quest Active Roles Snap-in installed     
  7. #2)Make sure you have Exchange Management Shell installed      
  8. #3)Load Quest ActiveRoles Snap-in                  
  9. #PS C:> Add-PSSnapin Quest.ActiveRoles.ADManagement    
  10. #4)CSV file should have first line as headers "username","domain"
  11. #
  12. #Creates Exchange mailboxes based off of a CSV providing username and domain values. Supports different domains if trust is established ahead of time... Outputs to a path defined by user.
  13. #####################################################################
  14.  
  15. ###############
  16. #Set variables#
  17. ###############
  18. $storageGroup = "ExchangeServerName\Storage Group Name\StorageGroupFileName"
  19. $logFile = "PathToOutputLogTo\MailboxCreation_{0:yyyyMMdd_HHmm}.log" -f (Get-Date)
  20. $CSVPath = "PathToCSVFile\exchangeusers.csv"
  21.  
  22. ##################
  23. #Custom Functions#
  24. ##################
  25. Function LogWrite ([string]$logString)
  26. {
  27.    Add-content $logFile -value $logString
  28. }
  29.  
  30. Function ValidationCheck ($importedUsers)
  31. {
  32.  
  33.     #Create an array for users to be processed
  34.     [array]$ltp = $null;
  35.    
  36.     #Log formatting :-)
  37.     LogWrite "*Validation Check*"
  38.     LogWrite "----------------"
  39.    
  40.    
  41.     ForEach($importedUser in $importedUsers){
  42.        
  43.         Try {
  44.            
  45.             switch($importedUser.domain) {
  46.                 "domain1" {
  47.                     $adAcct = Get-ADUser -Identity $importedUser.username -Server domain1.com
  48.                 }
  49.                          
  50.                 "domain2" {
  51.                     $adAcct = Get-ADUser -Identity $importedUser.username -Server domain2.com
  52.                 }
  53.  
  54.                 default {LogWrite "$($importedUser.username) did not have a valid domain name, check the CSV"}
  55.             }  
  56.  
  57.             If ($adAcct -ne $null) {
  58.                 #Check Exchange for existing mailbox
  59.                 If($uMailbox = Get-Mailbox -Identity $importedUser.username) {
  60.                         #Found existing mailbox, log it, don't add to list
  61.                         LogWrite "$($importedUser.username) already has a mailbox... skip."
  62.                 }
  63.                 Else {
  64.                         #Mailbox doesn't exist, add user to the list of boxes to process
  65.                         $ltp += $importedUser
  66.                         LogWrite "$($importedUser.username) will be processed."
  67.                 }
  68.             }
  69.         }
  70.         Catch {
  71.  
  72.             LogWrite "$_ check AD and confirm a user account exists"
  73.         }
  74.  
  75.        
  76.     }
  77.    
  78.     #Log formatting :-)
  79.     LogWrite ""
  80.    
  81.     return $ltp
  82. }
  83.  
  84. ######################
  85. #Import New Users CSV#
  86. ######################
  87.  
  88. Try
  89. {
  90.     #Log formatting :-)
  91.     LogWrite "*Mailbox Creation Script*"
  92.     LogWrite "----------------"
  93.     LogWrite "*CSV Import Process*"
  94.     LogWrite "----------------"
  95.    
  96.     #Import CSV
  97.     $importedUsers = Import-Csv $CSVPath
  98.    
  99.     #Clean up blank username records
  100.     $importedUsers = @($importedUsers | Where-Object {$_.username -ne ""})
  101.    
  102.     LogWrite "Users imported: $($importedUsers.Count)"
  103.     LogWrite "Import complete!"
  104.     LogWrite "----------------"
  105. }
  106. Catch
  107. {
  108.     #Write to log file
  109.     LogWrite "Failed to import CSV..." [System.Exception]
  110. }
  111.  
  112. ###########################
  113. #Scan imported data errors#
  114. ###########################
  115.  
  116. #Store results of the validation in an array, defined as an array because if not and only one result is returned, PoSH decides to treat it as a string
  117. [array]$fList = $null
  118. $fList = ValidationCheck $importedUsers
  119.  
  120. #Display final list count
  121. if ($fList.Count -gt 0){
  122.     LogWrite "Users to process: $($fList.Count)"
  123. }
  124. else{
  125.     LogWrite "Users to process: 0"
  126. }
  127. LogWrite "----------------"
  128.  
  129.  
  130. ##################
  131. #Create Mailboxes#
  132. ##################
  133.  
  134. #Log formatting
  135. LogWrite "Mailbox Creation"
  136. LogWrite "----------------"
  137.  
  138.  
  139. if ($fList.Count -gt 0){
  140.  
  141.     ForEach($item in $fList){
  142.        
  143.             Try
  144.             {
  145.                
  146.                 #Build UPN (accepted by Enable-Mailbox for identity parameter, allows us to account for different domains
  147.                 Switch ($item.domain)
  148.                 {
  149.                     "domain1" {$upn = $item.username + '@domain1.com'}
  150.                     "domain2" {$upn = $item.username + '@domain2.com'}
  151.                     default {$upn = "Issue with switch case"}
  152.                 }
  153.                
  154.                 #Enable mailbox
  155.                 if(Enable-Mailbox -Identity $upn  -Database $storageGroup)
  156.                 {
  157.                     LogWrite "$($item.username) mailbox created succesfully!"
  158.                 }
  159.  
  160.             }
  161.             Catch
  162.             {
  163.                 LogWrite "$($item.username) had an error when creating the mailbox."
  164.             }
  165.         }
  166. }
  167. else{
  168.     LogWrite "No mailboxes to create!"
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement