onsdag den 13. juli 2011

Exchange 2010 SP1 hosting mode import user

I’m preparing for Exchange 2010 with service pack 1 in hosting mode. There is several new things to take into account, but one of the first things I ran into that was driving me crazy was how to “import” an existing user to an exchange Organization.

I started with some nasty PowerShell script I got off from a forum post but the result wasn’t really that good, but then I stumbled across the HMC migration tools for exchange 2010 sp1 and that got me kick started pretty well.

what I'm basically doing is first “importing” the user as a MailUser. This makes all the PowerShell commands “know” about the user and what Organization the user belongs and then we can assign a mailbox to the user. Complete with the correct plans etc.

You could use the scripts from the first forum post, and then mail-enable the user. That works, true. But the user wont have had the correct mailbox plan applied, and if you try Set-Mailbox -Identity $usermb -MailboxPlan $mailboxplan on the user you will get all kinds of annoying errors and warnings, so this is a lot cleaner. Now I just need to automate the process of getting legacyExchangeDN and adding it as an x500 address on the users after my “migration” ( Microsoft, you f***ing morrons creating a service pack that require us to uninstall exchange complete before applying Service pack 1)

# Init
$Org = $customer.code
$Organization = Get-Organization $customer.Code -ea 0
if(!$Organization){
$Organization = New-Organization -Name $customer.Code -DomainName $customer.PrimaryDomainName -Location da-DK -ProgramID HostingSample -OfferID 2
}

$ADOrg = [ADSI]("LDAP://" + $Organization.DistinguishedName)
$ExchCU = $ADOrg.msexchcu
$ExchOURoot = $ADOrg.msexchouroot

$UserAccountControlValue = 66048

$mailboxplan = get-mailboxplan -organization $Org | where-object {$_.isdefault -ilike ("true") }
$defmailboxplan = $mailboxplan.name

# Move user if needed
$ADUser = Get-ADUser $User.Username
$userDN = ("CN=" + $user.name + "," + $Organization.DistinguishedName)
if($ADUser.DistinguishedName -ne $userDN){
Move
-ADObject -Identity $ADUser.DistinguishedName -TargetPath $Organization.DistinguishedName -Server $PreferedDC
}

#Assign user to exchange Organization
$objUser = [ADSI]("LDAP://" + $ADUser.DistinguishedName)
$objUser.Put("msExchCU","$ExchCU")
$objUser.Put("msExchOURoot","$ExchOURoot")
$objUser.setInfo()

# Create mailbox if needed
$usermb = get-mailbox $ADUser.DistinguishedName -ea 0
if(!$usermb){
$temp = Enable-MailUser $ADUser.DistinguishedName -ExternalEmailAddress:$user.UPN
$temp | Enable-Mailbox
Set
-MailUser $ADUser.DistinguishedName -UserPrincipalName:$user.UPN
$usermb = get-mailbox $ADUser.DistinguishedName
}
$temp = Get-MailUser $user.UPN -Organization test02 -ea 0
if($temp){
$temp | Enable-Mailbox
$usermb = get-mailbox $ADUser.DistinguishedName
}

# Assign MailPlan
Set-Mailbox -Identity $usermb -MailboxPlan $mailboxplan

Ingen kommentarer:

Send en kommentar