Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #####################################################################
- #Title: Exchange Mailbox Creation
- #Authors: Michael Ansaldi
- #Date: 08/15/2012
- #
- #1)Make sure you have Quest Active Roles Snap-in installed
- #2)Make sure you have Exchange Management Shell installed
- #3)Load Quest ActiveRoles Snap-in
- #PS C:> Add-PSSnapin Quest.ActiveRoles.ADManagement
- #4)CSV file should have first line as headers "username","domain"
- #
- #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.
- #####################################################################
- ###############
- #Set variables#
- ###############
- $storageGroup = "ExchangeServerName\Storage Group Name\StorageGroupFileName"
- $logFile = "PathToOutputLogTo\MailboxCreation_{0:yyyyMMdd_HHmm}.log" -f (Get-Date)
- $CSVPath = "PathToCSVFile\exchangeusers.csv"
- ##################
- #Custom Functions#
- ##################
- Function LogWrite ([string]$logString)
- {
- Add-content $logFile -value $logString
- }
- Function ValidationCheck ($importedUsers)
- {
- #Create an array for users to be processed
- [array]$ltp = $null;
- #Log formatting :-)
- LogWrite "*Validation Check*"
- LogWrite "----------------"
- ForEach($importedUser in $importedUsers){
- Try {
- switch($importedUser.domain) {
- "domain1" {
- $adAcct = Get-ADUser -Identity $importedUser.username -Server domain1.com
- }
- "domain2" {
- $adAcct = Get-ADUser -Identity $importedUser.username -Server domain2.com
- }
- default {LogWrite "$($importedUser.username) did not have a valid domain name, check the CSV"}
- }
- If ($adAcct -ne $null) {
- #Check Exchange for existing mailbox
- If($uMailbox = Get-Mailbox -Identity $importedUser.username) {
- #Found existing mailbox, log it, don't add to list
- LogWrite "$($importedUser.username) already has a mailbox... skip."
- }
- Else {
- #Mailbox doesn't exist, add user to the list of boxes to process
- $ltp += $importedUser
- LogWrite "$($importedUser.username) will be processed."
- }
- }
- }
- Catch {
- LogWrite "$_ check AD and confirm a user account exists"
- }
- }
- #Log formatting :-)
- LogWrite ""
- return $ltp
- }
- ######################
- #Import New Users CSV#
- ######################
- Try
- {
- #Log formatting :-)
- LogWrite "*Mailbox Creation Script*"
- LogWrite "----------------"
- LogWrite "*CSV Import Process*"
- LogWrite "----------------"
- #Import CSV
- $importedUsers = Import-Csv $CSVPath
- #Clean up blank username records
- $importedUsers = @($importedUsers | Where-Object {$_.username -ne ""})
- LogWrite "Users imported: $($importedUsers.Count)"
- LogWrite "Import complete!"
- LogWrite "----------------"
- }
- Catch
- {
- #Write to log file
- LogWrite "Failed to import CSV..." [System.Exception]
- }
- ###########################
- #Scan imported data errors#
- ###########################
- #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
- [array]$fList = $null
- $fList = ValidationCheck $importedUsers
- #Display final list count
- if ($fList.Count -gt 0){
- LogWrite "Users to process: $($fList.Count)"
- }
- else{
- LogWrite "Users to process: 0"
- }
- LogWrite "----------------"
- ##################
- #Create Mailboxes#
- ##################
- #Log formatting
- LogWrite "Mailbox Creation"
- LogWrite "----------------"
- if ($fList.Count -gt 0){
- ForEach($item in $fList){
- Try
- {
- #Build UPN (accepted by Enable-Mailbox for identity parameter, allows us to account for different domains
- Switch ($item.domain)
- {
- "domain1" {$upn = $item.username + '@domain1.com'}
- "domain2" {$upn = $item.username + '@domain2.com'}
- default {$upn = "Issue with switch case"}
- }
- #Enable mailbox
- if(Enable-Mailbox -Identity $upn -Database $storageGroup)
- {
- LogWrite "$($item.username) mailbox created succesfully!"
- }
- }
- Catch
- {
- LogWrite "$($item.username) had an error when creating the mailbox."
- }
- }
- }
- else{
- LogWrite "No mailboxes to create!"
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement