Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. <#
  2. .SYNOPSIS
  3.     Build Import file for o365 mail enabled objects
  4. .DESCRIPTION
  5.     Export AD properties to Sync from resource forest Exchange 2010 to on-premise AD in preparation of migrating to O365.  
  6. .PARAMETER <paramName>
  7.    <Description of script parameter>
  8. .EXAMPLE
  9.  Generate the CSV file for export..
  10.  
  11.  $ExportNames = get-mailbox -organizationalunit "Finance" -recipienttypedetails LinkedMailbox -resultsize unlimited
  12.  .\Export-RFToAD.ps1 -identity $ExportNames  -acceptedDomain "Example.COM" -O365Domain "Contoso.OnMicrosoft.com" -department "Finance"
  13.  
  14. #>
  15.  
  16.  
  17. #Name (name)
  18. #DisplayName (displayname)
  19. #SamAccountName (SamAccountName)
  20. #WindowsEmailAddress (mail)
  21. #PrimarySMTPAddress (from ProxyAddresses)
  22. #LegacyExchangeDN (legacyExchangeDN)
  23. #EmailAddresses (proxyaddresses)
  24. #ExchangeGUID (msExchMailboxGUID)
  25. #GrantSendOnBehalfTo (publicDelegates)
  26. #ExternalEmailAddress (TargetAddress)
  27.  
  28. [CmdLetBinding()]
  29. param(
  30.     [parameter(Mandatory=$true)]
  31.     $Identity,
  32.     [parameter(Mandatory=$true)]
  33.     [string[]]$AcceptedDomains,
  34.     [parameter(Mandatory=$true)]
  35.     [string]$o365Domain,
  36.     [parameter()][string]$Department   #Only used in naming of the CSV at end.
  37.  
  38. )
  39.  
  40. $NotIsADLoaded = ((get-command "get-aduser" -ea SilentlyContinue) -eq $null)
  41. if ($NotIsADLoaded ) {Import-Module ActiveDirectory}
  42.  
  43. #If fed mailboxes, it will strip off SamAccountName
  44. if (!($identity[0] -is [string]) -and ($identity[0].samaccountname -ne $null)) {
  45.     $Identity = $identity | %{$_.samaccountname}
  46. }
  47.  
  48. if ($o365Domain -notlike "*@*") {
  49.     $o365Domain = "@"+$o365Domain
  50. }
  51.  
  52. $AcceptedDomains = ($AcceptedDomains | %{$_.trim()}) -join(";")
  53.  
  54. if ($identity -is [string] -or ($identity -is [array] -and $identity[0] -is [string])) {
  55.     write-host "Attempting to read users as SamAccountName"
  56.     $Found = $identity | sort-object | %{Get-aduser -identity $_ -properties name,displayname,samaccountname,mail,legacyExchangeDN,proxyaddresses,publicdelegates,targetaddress,msexchmailboxguid,telephonenumber,company,department}
  57. } elseif ($identity.samaccountname -ne $null -or ($identity -is [array] -and $identity[0].samaccountname -is [string])) {
  58.     write-host "Using SamAccountName property on each Object"
  59.     $Found = $identity | sort-object | %{Get-aduser -identity $_.SamAccountName -properties name,displayname,samaccountname,mail,legacyExchangeDN,proxyaddresses,publicdelegates,targetaddress,msexchmailboxguid,telephonenumber,company,department}
  60. }
  61.  
  62. if ($found) {
  63.     $ProcessedUsers =@()
  64.     if ($found -is [array]) {$FoundCount = $found.count } else {$FoundCount = 1};$Index =1
  65.     ForEach ($User in $Found | ?{$_.mail -ne $null}) {
  66.         write-progress -Activity "Reading User Information" -status $User.mail -PercentComplete (($Index / $FoundCount)*100);$Index++
  67.         if ($Department -eq $null) {$department = $user.department}
  68.         $mbx = get-mailbox $user.distinguishedname -ea silentlycontinue
  69.         if ($mbx -ne $null) {
  70.             #Create user object with basic account info.
  71.             $FilteredUser = $User | select name,displayname,mail,samaccountname,targetaddress,telephonenumber,department,company
  72.             $userMailDomain = $user.mail.split("@")[1]
  73.             if (!($AcceptedDomains -match $userMailDomain)) {
  74.                 write-host "invalid domain in MAIL field For ",$($user.Displayname),":" -NoNewline
  75.                 if ($user.proxyaddresses.count -gt 0) {
  76.                     $acceptedEmails = $user.proxyaddresses | ?{$_ -clike "SMTP:*" -and $AcceptedDomains -match $_.split("@")[1]}
  77.                     if ($acceptedEmails.count -gt 1) {
  78.                         $PrimarySMTP = $AcceptedEmails[0]
  79.                     } else {
  80.                         $PrimarySMTP = $AcceptedEmails
  81.                     }              
  82.                     $FilteredUser.mail = $PrimarySMTP
  83.                     write-host "`t setting to $PrimarySMTP"
  84.                 } else {
  85.                     write-host "`t no proxy address, assigning onMicrosoft domain."
  86.                     $FilteredUser.mail = $user.mail.split("@")[0]+$o365Domain
  87.                 }
  88.             }
  89.  
  90.             #Pull out only SMTP email addresses that are allowed in ProxyAddresses field.          
  91.             $FilteredUser_EmailAddresses = [array]($User.proxyaddresses | ?{$_ -like "smtp:*" -and ($AcceptedDomains -match $_.split("@")[1])} | %{$_.split(":")[1]})
  92.             #Add redirector email address to o365 domain
  93.             #$UserPrefix = "smtp:"+$User.Mail.split("@")[0]
  94.             $UserPrefix = "smtp:"+$user.SamAccountName.replace(" ","")
  95.             #$FilteredUser_emailaddresses += [array]$($UserPrefix+$o365Domain)
  96.             if ($o365Domain -notlike "*.mail.onmicrosoft.com") {
  97.                 $O365MAILDomain = $o365Domain.replace(".onmicrosoft",".mail.onMicrosoft")
  98.                 $FilteredUser_emailaddresses += [array]$($UserPrefix+$O365MAILDomain)
  99.             } elseif ($o365Domain -like "*.mail.onmicrosoft.com") {
  100.                 $O365MAILDomain = $o365Domain.replace(".mail.onmicrosoft",".onMicrosoft")
  101.                 $FilteredUser_emailaddresses += [array]$($UserPrefix+$O365MAILDomain)
  102.             }
  103.             #Add x500 for on-premise domain
  104.             $FilteredUser_emailaddresses += [array]$("X500:"+$User.LegacyExchangeDN)
  105.             #Add everything to the Filtered User object.
  106.             $FilteredUser | add-member -name "EmailAddresses" -membertype Noteproperty -value ($FilteredUser_EmailAddresses -join(";"))
  107.             #See if User has Delegates aand add their SMTP email address
  108.             if ($User.PublicDelegates) {
  109.                 $MyDelegates = $User.PublicDelegates | %{(Get-recipient $_ -erroraction silentlycontinue).primarysmtpaddress}
  110.                 $FilteredUser | add-member -name "publicDelegates" -membertype Noteproperty -value ("'"+($MyDelegates -join("';'")) +"'")
  111.             } else {
  112.                 $FilteredUser | add-member -name "publicDelegates" -membertype Noteproperty -value "''"
  113.             }
  114.  
  115.             if ($mbx.linkedmasteraccount -like "*\*") {
  116.                 #Add Linked Master Account info, but exclude the AD Domain info.
  117.                 $FilteredUser | add-member -name "LinkedMasterAccount" -membertype Noteproperty -value $mbx.LinkedMasterAccount.split("\")[1]
  118.             } Else {
  119.                 $FilteredUser | add-member -name "LinkedMasterAccount" -membertype Noteproperty -value ""
  120.             }
  121.             #$FilteredUser | add-member -name "CustomAttribute3" -membertype Noteproperty -value $mbx.CustomAttribute3
  122.             #$FilteredUser | add-member -name "CustomAttribute9" -membertype Noteproperty -value $mbx.CustomAttribute9
  123.             $FilteredUser | add-member -name "HiddenFromAddressListsEnabled" -membertype Noteproperty -value $mbx.HiddenFromAddressListsEnabled
  124.             $FilteredUser | add-member -name "LitigationHoldEnabled" -membertype Noteproperty -value $mbx.LitigationHoldEnabled
  125.             #Add the Exchange GUID value, in String not HEX format.
  126.             $FilteredUser | add-member -name "ExchangeGuid" -membertype Noteproperty -value $mbx.ExchangeGuid.tostring()
  127.             $FilteredUser | add-member -name "RecipientTypeDetails" -membertype Noteproperty -value $mbx.RecipientTypeDetails
  128.             $ProcessedUsers += $FilteredUser
  129.         } else {
  130.             write-host "ERROR: Mailbox not found for ",$user.displayname
  131.             $ErrorReportFilePath = "\\rf01\camail\Scripts\GalSync\"+$Department+"_MissingMailboxes_"+$datestr+".txt"
  132.             $user.displayname | Out-File -Append -FilePath $ErrorReportFilePath
  133.         }
  134.     }
  135.     #Create Output file.
  136.     $datestr = [string](Get-Date -UFormat "%m-%d-%y")
  137.     $FilePath = ".\"+$Department + "_RF01_Mailbox_Properties_"+$datestr + ".csv"
  138.     $ProcessedUsers | export-csv $FilePath -notypeinfo
  139.     write-host "completed. ",$FilePath
  140. } else {write-host "No users found"}