PowerShell을 사용하여 Exchange의 사용자 정보를 Office 365로 내보내기

5176 단어 Office365O365
오늘 PowerShell에 관한 기사를 소개합니다. 통상적인 의미에서 Exchange를 Office 365로 이전하려면 마이크로소프트 자체와 제3자를 포함한 여러 가지 방법이 있습니다. 만약에 우리가 제3자 도구를 통해 이전하고 싶다면 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를 지정할 수도 있습니다.

좋은 웹페이지 즐겨찾기