Create mailboxes from a CSV file

Takes a CSV file, reads headers and creates mailbox based on values.
Requires two columns in CSV, UserPrincipalname and Name. The UPN can be the SAMAccount value renamed. It will append the variable $UPNDomain to end.

Also requires both Quest ActiveRoles PowerShell tools (to create AD Account) and Exchange 2007 Management tools to set mailbox quota limits.

#CreateMailboxFromCSV.PS1
$file = "c:\List_Of-mailboxes.csv"
$Container = "ou=EricTest,ou=Mailbox Customers,dc=corp,dc=ent"
$UPNDomain = "@example.com"
$strMailServerName = '/o=CA/ou=First Administrative Group/cn=Configuration/cn=Servers/cn=EXCHSrvr1'
$strDatabaseName = 'CN=SG1-PRIV1,CN=SG1,CN=InformationStore,CN=EXCHSrvr1,CN=Servers,CN=First Administrative Group,CN=Administrative Groups,CN=CA,CN=Microsoft Exchange,CN=Services,CN=Configuration,dc=corp,dc=ent'

[array]$csv = import-csv $file

# This script implies that columns "UserPrincipalName", "name" exist in the csv file

if (($csv[0].UserPrincipalName -eq $null) -or ($csv[0].Name -eq $null) -or ($csv[0].Database -eq $null) ) {Throw “Parameter missing… Make sure the CSV file has the following columns: UserPrincipalName, Name, Database, OrganizationalUnit.”}

# Create collection of the commands that we will invoke in the end
[collections.arraylist]$Commands = new-object system.collections.arraylist

for($i = 0; $i -lt $csv.Count; $i ++ ) {
        if ($csv[$i].UserPrincipalName.contains("@")) {
                [void]$Commands.Add($objNewUser =' + ” new-qaduser -UserPrincipalName `”$($csv[$i].UserPrincipalName)`” -Name `”$($csv[$i].Name)`” -ParentContainer `”$($Container)`”" + ‘ -Password $pwd ‘)
        } else {
                [void]$Commands.Add(‘$objNewUser ='
+ ” new-qaduser -UserPrincipalName `”$($csv[$i].UserPrincipalName+$UPNDomain)`” -Name `”$($csv[$i].Name)`” -ParentContainer `”$($Container)`”" + ‘ -Password $pwd ‘)
        }
}

# Add other parameters if present in the CSV

if ($csv[0].Alias -ne $null) {
        for($i = 0; $i -lt $csv.Count; $i ++ ) {
                $Commands[$i] = $Commands[$i] + ” -Alias `”$($csv[$i].Alias)`”"

        }
}

if ($csv[0].DisplayName -ne $null) {
        for($i = 0; $i -lt $csv.Count; $i ++ ) {
                #clean up display name value of extra characters
                $DNameStr = $csv[$i].DisplayName.Replace('`','')
                #write-host $dnamestr
                $Commands[$i] = $Commands[$i] + ” -displayname `”$($DNameStr)`”"
        }
}


if (($csv[0].FirstName -ne $null) -or ($csv[0].GivenName -ne $null)) {
        for($i = 0; $i -lt $csv.Count; $i ++ ) {
                if ($csv[0].FirstName -ne $null) {
                        $Commands[$i] = $Commands[$i] + ” -FirstName `”$($csv[$i].FirstName)`”"
                } else {
                        $Commands[$i] = $Commands[$i] + ” -FirstName `”$($csv[$i].GivenName)`”"
                }
        }
}


if (($csv[0].LastName -ne $null) -or ($csv[0].sn -ne $null)) {
        for($i = 0; $i -lt $csv.Count; $i ++ ) {
                if ($csv[0].LastName -ne $null) {
                        $Commands[$i] = $Commands[$i] + ” -LastName `”$($csv[$i].LastName)`”"
                } else {
                        $Commands[$i] = $Commands[$i] + ” -LastName `”$($csv[$i].sn)`”"
                }
        }
}


if ($csv[0].Initials -ne $null) {
        for($i = 0; $i -lt $csv.Count; $i ++ ) {
                $Commands[$i] = $Commands[$i] + ” -Initials `”$($csv[$i].Initials)`”"
        }
}

if ($csv[0].telephonenumber -ne $null) {
        for($i = 0; $i -lt $csv.Count; $i ++ ) {
                $Commands[$i] = $Commands[$i] + ” -PhoneNumber `”$($csv[$i].telephonenumber)`”"
        }
}

if ($csv[0].faxnumber -ne $null) {
        for($i = 0; $i -lt $csv.Count; $i ++ ) {
                $Commands[$i] = $Commands[$i] + ” -fax `”$($csv[$i].faxnumber)`”"
        }
}

if ($csv[0].department -ne $null) {
        for($i = 0; $i -lt $csv.Count; $i ++ ) {
                $Commands[$i] = $Commands[$i] + ” -department `”$($csv[$i].department)`”"
        }
}

if (($csv[0].SamAccountName -ne $null) -or ($csv[0].UserPrincipalName -ne $null)) {
        for($i = 0; $i -lt $csv.Count; $i ++ ) {
                if ($csv[0].SamAccountName -ne $null) {
                        $strSam = $csv[$i].SamAccountName
                } else {
                        $strSam = $csv[$i].UserPrincipalName
                        $intAt = $strSam.indexof("@")
                        if ($intAT -ne -1) {
                                $strSam = $strSam.substring(0,$intAt)
                        }
                }
                $Commands[$i] = $Commands[$i] + ” -SamAccountName `”$($strSam)`”"
        }
}


for($i = 0; $i -lt $csv.Count; $i ++ ) {
        $pwd = new-object Security.SecureString

        if ($csv[0].Password -ne $null) {
                $csv[$i].Password.ToCharArray() | foreach { $pwd.AppendChar($_) }
        } else {
                #$csv[$i].UserPrincipalName.ToCharArray() | foreach { $pwd.AppendChar($_)}
                $a = new-object random
                $b = [string]$a.next()
                $B.toCharArray() |      foreach { $pwd.AppendChar($_)}
        }
        write-host $Commands[$i]           #varibable is assigned when ran.
        Invoke-Expression $Commands[$i]
               
        ##http://www.powergui.org/thread.jspa?messageID=11753&#11753
        $LastMbx = $objNewuser.DN
        $ldapquery = "LDAP://" + $objNewuser.DN
        $newmbx = [ADSI]$ldapquery
        $newmbx.mailNickname = $csv[$i].mailnickname
        $newmbx.msExchHomeServerName = $strMailServerName
        $newmbx.homeMDB = $strDatabaseName
       
        if ($csv[0].mail -ne $null) {
                #$strEmailAddress =
        $newmbx.Mail = $csv[$i].mail
        }
       
        if ($csv[0].proxyaddresses -ne $null) {
                $strProxy = $csv[$i].proxyaddresses
                $strProxy = $strProxy.Replace(";;",";")
                $arrProxy = $strProxy.split(";")
        $newmbx.proxyaddresses = $arrProxy
        }
                       
        $newmbx.setinfo()              
}

#### If you don'
t have the Exchange 2007 management tools, Delete the rest of the script..

"Waiting for Mailbox GUID to populate"

do {
        $a = Get-QADUser $lastmbx -IncludedProperties 'msexchmailboxguid'  | select msexchmailboxguid  
        $b = [string]$a.msexchmailboxguid
} while ($b.Length -eq 0)

"I just found:" + $b

if ($csv[0].mdbQuota -ne $null) {
        for($i = 0; $i -lt $csv.Count; $i ++ ) {
                $csv[$i].UserPrincipalName
                get-mailbox $csv[$i].UserPrincipalName | Set-mailbox  –prohibitsendquota $csv[$i].mdbQuota -IssueWarningQuota $csv[$i].mbdLimit -UseDatabaseQuotaDefaults $FALSE

        }
}

Comments

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <blockquote> <center> <hr> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <img>
  • Lines and paragraphs break automatically.
  • Web page addresses and e-mail addresses turn into links automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <drupal6>, <html>, <java>, <javascript>, <php>, <posh>.

More information about formatting options

Type the characters you see in this picture. (verify using audio)
Type the characters you see in the picture above; if you can't read them, submit the form and a new image will be generated. Not case sensitive.