PowerShell을 사용하여 Exchange의 사용자 정보를 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를 지정할 수도 있습니다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Office 365 License 자동 할당텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.