今天来介绍一篇关于PowerShell的文章,通常意义上来说我们如果想迁移Exchange到Office 365的话,可以有很多种方法,包括微软自己的以及第三方的,如果我们想通过第三方工具迁移的话,可以通过AAD Connect来将用户数据同步到Office 365, 当然,这种方法后续还需要一些别的操作才能让邮箱创建出来,如果不通过AAD Connect,我们也可以直接将Exchange里的一些信息从服务器中导出来,然后通过CSV的方式直接在Office 365里创建。
今天来分享一个自己用的将用户信息从Office 365里导出来的脚本,运行很简单,下边把代码分享一下
param ( [parameter(ValueFromPipeline = $true)] $OUPath, [string]$DomainName, [string]$ForO365ExportTo = [Environment]::GetFolderPath("Desktop") + "\" + "UserInfoForO365FromExchange.csv" ) try { $Error.clear() Import-Module ActiveDirectory -ErrorAction 'Stop' $name = (Get-WmiObject -Class Win32_ComputerSystem).name + "." + (Get-WmiObject -Class Win32_ComputerSystem).domain $PsSessionAdded = $false Get-PSSession | %{ if ($_.ComputerName -eq $name) { $PsSessionAdded = $true } } if ($PsSessionAdded -eq $false) { $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri ("http://" + $name + "/PowerShell/") -Authentication Kerberos -ErrorAction 'Stop' Import-PSSession $Session -ErrorAction 'Stop' | Out-Null } } catch { throw $Error[0].Exception.Message } Function Test-OUPath() { param ([string]$path) $OUExists = [adsi]::Exists("LDAP://$path") return $OUExists } if (([string]::IsNullOrEmpty($DomainName) -or ([string]::IsNullOrWhiteSpace($DomainName)))) { throw "$(Get-Date) * Please provide your domain name" } if ($OUPath) { #Get Mailbox with OUPath if (Test-OUPath $OUPath) { $Mailbox = Get-Mailbox -Filter { RecipientTypeDetails -eq "UserMailbox" } -ResultSize Unlimited -OrganizationalUnit $OUPath } else { Write-Warning "$(Get-Date) * $OUPath does not exist, please check" exit } } else { #Get all Mailboxes $Mailbox = Get-Mailbox -Filter { RecipientTypeDetails -eq "UserMailbox" } -ResultSize Unlimited } [pscustomobject[]]$O365UserObjects = $null if ($OUPath) { Write-Host "$(Get-Date) * The OU is $OUPath, begin to collect information, please wait..." } else { Write-Host "$(Get-Date) * No OU provided, begin to collect information of all mailboxes, please wait..." } $Mailbox | %{ #============================================================================ #For O365 User provision $User = Get-ADuser -Identity $_.SamAccountName -Properties * $UserName = $User.UserPrincipalName $UserName = $UserName.Split("@")[0] $UserName = $UserName + "@" + $DomainName $FirstName = $User.GivenName $LastName = $User.Surname $DisplayName = $User.DisplayName $JobTitle = $User.Title $Department = $User.Department $OfficeNumber = "" $OfficePhone = $User.OfficePhone $MobilePhone = $User.MobilePhone $Fax = $User.Fax $Address = $User.StreetAddress $City = $User.City $State = $User.State $ZipCode = $User.PostalCode $Country = $User.Country #============================================================================ $O365UserObject = New-Object -TypeName psobject $O365UserObject | Add-Member -MemberType NoteProperty -Name 'User Name' -Value $UserName $O365UserObject | Add-Member -MemberType NoteProperty -Name 'First Name' -Value $FirstName $O365UserObject | Add-Member -MemberType NoteProperty -Name 'Last Name' -Value $LastName $O365UserObject | Add-Member -MemberType NoteProperty -Name 'Display Name' -Value $DisplayName $O365UserObject | Add-Member -MemberType NoteProperty -Name 'Job Title' -Value $JobTitle $O365UserObject | Add-Member -MemberType NoteProperty -Name 'Department' -Value $Department $O365UserObject | Add-Member -MemberType NoteProperty -Name 'Office Number' -Value $OfficeNumber $O365UserObject | Add-Member -MemberType NoteProperty -Name 'Office Phone' -Value $OfficePhone $O365UserObject | Add-Member -MemberType NoteProperty -Name 'Mobile Phone' -Value $MobilePhone $O365UserObject | Add-Member -MemberType NoteProperty -Name 'Fax' -Value $Fax $O365UserObject | Add-Member -MemberType NoteProperty -Name 'Address' -Value $Address $O365UserObject | Add-Member -MemberType NoteProperty -Name 'City' -Value $City $O365UserObject | Add-Member -MemberType NoteProperty -Name 'State or Province' -Value $State $O365UserObject | Add-Member -MemberType NoteProperty -Name 'ZIP or Postal Code' -Value $ZipCode $O365UserObject | Add-Member -MemberType NoteProperty -Name 'Country or Region' -Value $Country $O365UserObjects += $O365UserObject } if ($O365UserObjects -ne $null) { try { $Error.clear() $O365UserObjects | Export-Csv -NoTypeInformation -Append -LiteralPath $ForO365ExportTo -Force -ErrorAction 'Stop' Write-Host "$(Get-Date) * Done. Please check $ForO365ExportTo" } catch { Write-Warning $Error[0].Exception.Message } } else { Write-Warning "$(Get-Date) * Something wrong, didn't get any info" }
运行方法就不介绍了,本身参数其实很少,也可以指定导出的OU
原文地址:http://blog.51cto.com/mxyit/2092839
时间: 2024-10-07 04:54:09