1. 程式人生 > >使用PowerShell 導出Exchange中的用戶中用戶信息到Office 365

使用PowerShell 導出Exchange中的用戶中用戶信息到Office 365

Office 365 O365 Exchange PowerShell csv

今天來介紹一篇關於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

使用PowerShell 導出Exchange中的用戶中用戶信息到Office 365