<#
.SYNOPSIS
Build Import file for o365 mail enabled objects
.DESCRIPTION
Export AD properties to Sync from resource forest Exchange 2010 to on-premise AD in preparation of migrating to O365.
.PARAMETER <paramName>
<Description of script parameter>
.EXAMPLE
Generate the CSV file for export..
$ExportNames = get-mailbox -organizationalunit "Finance" -recipienttypedetails LinkedMailbox -resultsize unlimited
.\Export-RFToAD.ps1 -identity $ExportNames -acceptedDomain "Example.COM" -O365Domain "Contoso.OnMicrosoft.com" -department "Finance"
#>
#Name (name)
#DisplayName (displayname)
#SamAccountName (SamAccountName)
#WindowsEmailAddress (mail)
#PrimarySMTPAddress (from ProxyAddresses)
#LegacyExchangeDN (legacyExchangeDN)
#EmailAddresses (proxyaddresses)
#ExchangeGUID (msExchMailboxGUID)
#GrantSendOnBehalfTo (publicDelegates)
#ExternalEmailAddress (TargetAddress)
[CmdLetBinding()]
param(
[parameter(Mandatory=$true)]
$Identity,
[parameter(Mandatory=$true)]
[string[]]$AcceptedDomains,
[parameter(Mandatory=$true)]
[string]$o365Domain,
[parameter()][string]$Department #Only used in naming of the CSV at end.
)
$NotIsADLoaded = ((get-command "get-aduser" -ea SilentlyContinue) -eq $null)
if ($NotIsADLoaded ) {Import-Module ActiveDirectory}
#If fed mailboxes, it will strip off SamAccountName
if (!($identity[0] -is [string]) -and ($identity[0].samaccountname -ne $null)) {
$Identity = $identity | %{$_.samaccountname}
}
if ($o365Domain -notlike "*@*") {
$o365Domain = "@"+$o365Domain
}
$AcceptedDomains = ($AcceptedDomains | %{$_.trim()}) -join(";")
if ($identity -is [string] -or ($identity -is [array] -and $identity[0] -is [string])) {
write-host "Attempting to read users as SamAccountName"
$Found = $identity | sort-object | %{Get-aduser -identity $_ -properties name,displayname,samaccountname,mail,legacyExchangeDN,proxyaddresses,publicdelegates,targetaddress,msexchmailboxguid,telephonenumber,company,department}
} elseif ($identity.samaccountname -ne $null -or ($identity -is [array] -and $identity[0].samaccountname -is [string])) {
write-host "Using SamAccountName property on each Object"
$Found = $identity | sort-object | %{Get-aduser -identity $_.SamAccountName -properties name,displayname,samaccountname,mail,legacyExchangeDN,proxyaddresses,publicdelegates,targetaddress,msexchmailboxguid,telephonenumber,company,department}
}
if ($found) {
$ProcessedUsers =@()
if ($found -is [array]) {$FoundCount = $found.count } else {$FoundCount = 1};$Index =1
ForEach ($User in $Found | ?{$_.mail -ne $null}) {
write-progress -Activity "Reading User Information" -status $User.mail -PercentComplete (($Index / $FoundCount)*100);$Index++
if ($Department -eq $null) {$department = $user.department}
$mbx = get-mailbox $user.distinguishedname -ea silentlycontinue
if ($mbx -ne $null) {
#Create user object with basic account info.
$FilteredUser = $User | select name,displayname,mail,samaccountname,targetaddress,telephonenumber,department,company
$userMailDomain = $user.mail.split("@")[1]
if (!($AcceptedDomains -match $userMailDomain)) {
write-host "invalid domain in MAIL field For ",$($user.Displayname),":" -NoNewline
if ($user.proxyaddresses.count -gt 0) {
$acceptedEmails = $user.proxyaddresses | ?{$_ -clike "SMTP:*" -and $AcceptedDomains -match $_.split("@")[1]}
if ($acceptedEmails.count -gt 1) {
$PrimarySMTP = $AcceptedEmails[0]
} else {
$PrimarySMTP = $AcceptedEmails
}
$FilteredUser.mail = $PrimarySMTP
write-host "`t setting to $PrimarySMTP"
} else {
write-host "`t no proxy address, assigning onMicrosoft domain."
$FilteredUser.mail = $user.mail.split("@")[0]+$o365Domain
}
}
#Pull out only SMTP email addresses that are allowed in ProxyAddresses field.
$FilteredUser_EmailAddresses = [array]($User.proxyaddresses | ?{$_ -like "smtp:*" -and ($AcceptedDomains -match $_.split("@")[1])} | %{$_.split(":")[1]})
#Add redirector email address to o365 domain
#$UserPrefix = "smtp:"+$User.Mail.split("@")[0]
$UserPrefix = "smtp:"+$user.SamAccountName.replace(" ","")
#$FilteredUser_emailaddresses += [array]$($UserPrefix+$o365Domain)
if ($o365Domain -notlike "*.mail.onmicrosoft.com") {
$O365MAILDomain = $o365Domain.replace(".onmicrosoft",".mail.onMicrosoft")
$FilteredUser_emailaddresses += [array]$($UserPrefix+$O365MAILDomain)
} elseif ($o365Domain -like "*.mail.onmicrosoft.com") {
$O365MAILDomain = $o365Domain.replace(".mail.onmicrosoft",".onMicrosoft")
$FilteredUser_emailaddresses += [array]$($UserPrefix+$O365MAILDomain)
}
#Add x500 for on-premise domain
$FilteredUser_emailaddresses += [array]$("X500:"+$User.LegacyExchangeDN)
#Add everything to the Filtered User object.
$FilteredUser | add-member -name "EmailAddresses" -membertype Noteproperty -value ($FilteredUser_EmailAddresses -join(";"))
#See if User has Delegates aand add their SMTP email address
if ($User.PublicDelegates) {
$MyDelegates = $User.PublicDelegates | %{(Get-recipient $_ -erroraction silentlycontinue).primarysmtpaddress}
$FilteredUser | add-member -name "publicDelegates" -membertype Noteproperty -value ("'"+($MyDelegates -join("';'")) +"'")
} else {
$FilteredUser | add-member -name "publicDelegates" -membertype Noteproperty -value "''"
}
if ($mbx.linkedmasteraccount -like "*\*") {
#Add Linked Master Account info, but exclude the AD Domain info.
$FilteredUser | add-member -name "LinkedMasterAccount" -membertype Noteproperty -value $mbx.LinkedMasterAccount.split("\")[1]
} Else {
$FilteredUser | add-member -name "LinkedMasterAccount" -membertype Noteproperty -value ""
}
#$FilteredUser | add-member -name "CustomAttribute3" -membertype Noteproperty -value $mbx.CustomAttribute3
#$FilteredUser | add-member -name "CustomAttribute9" -membertype Noteproperty -value $mbx.CustomAttribute9
$FilteredUser | add-member -name "HiddenFromAddressListsEnabled" -membertype Noteproperty -value $mbx.HiddenFromAddressListsEnabled
$FilteredUser | add-member -name "LitigationHoldEnabled" -membertype Noteproperty -value $mbx.LitigationHoldEnabled
#Add the Exchange GUID value, in String not HEX format.
$FilteredUser | add-member -name "ExchangeGuid" -membertype Noteproperty -value $mbx.ExchangeGuid.tostring()
$FilteredUser | add-member -name "RecipientTypeDetails" -membertype Noteproperty -value $mbx.RecipientTypeDetails
$ProcessedUsers += $FilteredUser
} else {
write-host "ERROR: Mailbox not found for ",$user.displayname
$ErrorReportFilePath = "\\rf01\camail\Scripts\GalSync\"+$Department+"_MissingMailboxes_"+$datestr+".txt"
$user.displayname | Out-File -Append -FilePath $ErrorReportFilePath
}
}
#Create Output file.
$datestr = [string](Get-Date -UFormat "%m-%d-%y")
$FilePath = ".\"+$Department + "_RF01_Mailbox_Properties_"+$datestr + ".csv"
$ProcessedUsers | export-csv $FilePath -notypeinfo
write-host "completed. ",$FilePath
} else {write-host "No users found"}