Como exportar grupos de distribuição e seus membros para um arquivo CSV no Office 365

Shape Image One
Como exportar grupos de distribuição e seus membros para um arquivo CSV no Office 365

Gostaria de tornar-se um especialista em Office 365? Então recomendo que conheça o Curso Online de Office 365 para administradores oferecido pela MW AcademyClique aqui e saiba mais,
Salve Salve galera! Este artigo visa descrever como exportar os grupos de distribuição e seus respectivos membros diretamente para um arquivo CSV no Office 365 através do Power Shell;


1) Salve o script abaixo como .ps1 e execute via Power Shell, faça login com suas credenciais e aguarde que o script gere o arquivo CSV com os grupos e usuários pertencentes ao mesmo;


#Accept input parameters 
Param(
[Parameter(Position=0, Mandatory=$false, ValueFromPipeline=$true)]
[string] $Office365Username,
[Parameter(Position=1, Mandatory=$false, ValueFromPipeline=$true)]
[string] $Office365Password
)

#Constant Variables
$OutputFile = "DistributionGroupMembers.csv" #The CSV Output file that is created, change for your purposes
$arrDLMembers = @{}


#Remove all existing Powershell sessions
Get-PSSession | Remove-PSSession

#Did they provide creds? If not, ask them for it.
if (([string]::IsNullOrEmpty($Office365Username) -eq $false) -and ([string]::IsNullOrEmpty($Office365Password) -eq $false))
{
$SecureOffice365Password = ConvertTo-SecureString -AsPlainText $Office365Password -Force

#Build credentials object
$Office365Credentials = New-Object System.Management.Automation.PSCredential $Office365Username, $SecureOffice365Password
}
else
{
#Build credentials object
$Office365Credentials = Get-Credential
}
#Create remote Powershell session
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell -Credential $Office365credentials -Authentication Basic –AllowRedirection

#Import the session
Import-PSSession $Session -AllowClobber | Out-Null

#Prepare Output file with headers
Out-File -FilePath $OutputFile -InputObject "Distribution Group DisplayName,Distribution Group Email,Member DisplayName, Member Email, Member Type" -Encoding UTF8

#Get all Distribution Groups from Office 365
$objDistributionGroups = Get-DistributionGroup -ResultSize Unlimited

#Iterate through all groups, one at a time
Foreach ($objDistributionGroup in $objDistributionGroups)
{

write-host "Processing $($objDistributionGroup.DisplayName)..."

#Get members of this group
$objDGMembers = Get-DistributionGroupMember -Identity $($objDistributionGroup.PrimarySmtpAddress)

write-host "Found $($objDGMembers.Count) members..."

#Iterate through each member
Foreach ($objMember in $objDGMembers)
{
Out-File -FilePath $OutputFile -InputObject "$($objDistributionGroup.DisplayName),$($objDistributionGroup.PrimarySMTPAddress),$($objMember.DisplayName),$($objMember.PrimarySMTPAddress),$($objMember.RecipientType)" -Encoding UTF8 -append
write-host "`t$($objDistributionGroup.DisplayName),$($objDistributionGroup.PrimarySMTPAddress),$($objMember.DisplayName),$($objMember.PrimarySMTPAddress),$($objMember.RecipientType)"
}
}

#Clean up session
Get-PSSession | Remove-PSSession

Deixe um comentário

O seu endereço de e-mail não será publicado.